Showing posts with label bind. Show all posts
Showing posts with label bind. Show all posts

Wednesday, March 7, 2012

ALTER DATABASE dynamic SQL

I am in a bind.
I am writing a stored proc that needs to change the logical name of a
database as one step in the process. The issue is that I can't seem to pass
the database in a variable to the Alter Database command.
What are the options I have for making this work?
Code snippet:
DECLARE @.NewDB
@.NewDB = 'TestDB'
@.NewLogicalName = 'TestDB'
@.OldLogicalName = 'TemplateDB'
ALTER DATABASE @.NewDB <-- This generates an error
MODIFY FILE (
NAME = @.OldLogicalName,
NEWNAME = @.NewLogicalName
)
Thanks!!!!!
RSHRSH wrote:
> I am in a bind.
> I am writing a stored proc that needs to change the logical name of a
> database as one step in the process. The issue is that I can't seem to pa
ss
> the database in a variable to the Alter Database command.
> What are the options I have for making this work?
> Code snippet:
> DECLARE @.NewDB
> @.NewDB = 'TestDB'
> @.NewLogicalName = 'TestDB'
> @.OldLogicalName = 'TemplateDB'
> ALTER DATABASE @.NewDB <-- This generates an error
> MODIFY FILE (
> NAME = @.OldLogicalName,
> NEWNAME = @.NewLogicalName
> )
> Thanks!!!!!
> RSH
You will have to use EXEC for that. For example:
EXEC (@.alter_stmt)
Where @.alter_stmt is a string containing your ALTER DATABASE statement.
But why would you want to do such a thing in a proc?
David Portas
SQL Server MVP
--|||Thanks.
It is a long story but basically we have template databases and when a new
company is created we backup the template database and restore it to a new
name. It works good and everything changes except the Logical name so i
needed a way to change the logical name to the new database name.
This stems from the fact that our application was built using Access
databases in which this was standard procedure. We are moving over to SQL
and unfortunately we can't justify writing a few thousand lines of
application code, so we are stuck bending a few best practices here and
there to initially get things running.
Thanks,
RSH
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1133963892.610144.63210@.z14g2000cwz.googlegroups.com...
> RSH wrote:
>
> You will have to use EXEC for that. For example:
> EXEC (@.alter_stmt)
> Where @.alter_stmt is a string containing your ALTER DATABASE statement.
> But why would you want to do such a thing in a proc?
> --
> David Portas
> SQL Server MVP
> --
>|||RSH wrote:
> This stems from the fact that our application was built using Access
> databases in which this was standard procedure. We are moving over to SQL
> and unfortunately we can't justify writing a few thousand lines of
> application code
Experience suggests this is a recipe for failure. Virtually every
Access database I've ever seen is incompatible with good design in SQL
Server (or in most other database systems for that matter!). Wait until
you can justify rewriting it is my advice. No solution at all is
frequently better than a bad solution.
Good luck.
David Portas
SQL Server MVP
--|||> It is a long story but basically we have template databases and when a new
> company is created we backup the template database and restore it to a new
> name.
I use model for this. Life is much simpler (except when I need to create a
database that's not based on the template).
A|||RSH (way_beyond_oops@.yahoo.com) writes:
> It is a long story but basically we have template databases and when a new
> company is created we backup the template database and restore it to a new
> name. It works good and everything changes except the Logical name so i
> needed a way to change the logical name to the new database name.
But why would you worry about the logical name? If all files in all
databases have the same logical name, so what? Easier to manage, because
you always know what the names are. :-)
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||If the database named SALES_GA_2005 had a logical file name of SALES_FL and
physical name of SALES_2001.MDF, it could be confusing for the administrator
who manages the server.
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns9725AFA87588BYazorman@.127.0.0.1...
> RSH (way_beyond_oops@.yahoo.com) writes:
> But why would you worry about the logical name? If all files in all
> databases have the same logical name, so what? Easier to manage, because
> you always know what the names are. :-)
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||I fixed it by calling the stored procedure Alter Database from my
application rather than parameterize it in a SP.
I agree in a perfect world it would be nice to re layout the DBs from
scratch and modify the application logic to utilize it...but the fact of the
matter is the people with the wallets want a solution and while it isn't
perfect it will work for the meantime. We are going to be porting our
application to .Net next year and that is when we are going to be revamping
things to take advantage of SQL server but until then we have to keep
generating revenue.
The logical name needs to be changed because I will still be referencing the
template database by name and I can't very well have 100 logical names all
the same...that would be quite an administrative nightmare.
Thanks for the help and insight.
:-)
"JT" <someone@.microsoft.com> wrote in message
news:eB$Odu0%23FHA.3464@.TK2MSFTNGP15.phx.gbl...
> If the database named SALES_GA_2005 had a logical file name of SALES_FL
> and physical name of SALES_2001.MDF, it could be confusing for the
> administrator who manages the server.
> "Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
> news:Xns9725AFA87588BYazorman@.127.0.0.1...
>|||> If the database named SALES_GA_2005 had a logical file name of SALES_FL
> and physical name of SALES_2001.MDF, it could be confusing for the
> administrator who manages the server.
Well, the intent was probably more like giving the logical file names a more
generic descriptor, like "data" and "log"... of course the MDF/LDF file
names would have to be unique, unless each pair is in its own folder.|||RSH wrote:
> The logical name needs to be changed because I will still be referencing t
he
> template database by name and I can't very well have 100 logical names all
> the same...that would be quite an administrative nightmare.
>
Are you sure you understand what the logical file name is? A database
is never referenced by its logical file name (for one reason because
there are at least two logical file names per database). The only time
you reference the logical name is if you need to change one of the file
properties such as size, location, growth, etc. In that case you always
need the database name as well. So I can't make sense of why it would
be an "administrative nightmare" to have the same names in each DB. In
some ways it makes things easier if you do this stuff dynamically.
I do agree that it makes sense to have a good naming convention but I'm
not convinced you understand what the logical file name is for.
David Portas
SQL Server MVP
--

Thursday, February 16, 2012

Allow reference to dynamically-created DataColumn from reportHelp?

Hi everyone,

I currently have a strongly-typed dataset that, in code, will expand tables according to relationships in that dataset in order to bind it to my ReportViewer. My problem now is whenever I try to run it, I get an error looking like the following:

An error occurred during local report processing.
The definition of the report 'Main Report' is invalid.
The Value expression for the textbox `textbox15` refers to the field `Parent_FullName`. Report item expressions can only refer to fields within the current data set scope or, if inside an aggregate, the specified data scope.

The reason I'm dynamically creating this dataset on the fly is because I couldn't find how to do a one-to-one relationship (bind a column in one report table to a column in a different dataset table than everything else on that report table). I realize the problem is that in the report's "DataSets" node, it doesn't include a definition of these generated columns... but is there any way around this?

Time is of the essence, but I'd appreciate any help at all!

TIA! =)

Hello,

You may want to look into using calculated fields. These allow you to use expressions as the source of data fields and are used just like regular fields.

For more information about calculated fields:
http://msdn2.microsoft.com/en-us/library/ms156295.aspx

Also, you should find more helpful information if you search for "calculated fields" in this forum and the web.

Ian|||

Hi Ian,

That's exactly what I'm doing. When the dataset is passed in to the form, I have the form automatically expand on its existing relationships and add calculated fields to the datatables at runtime. I can't do this at design-time because I'm using this one form to eventually run 100's of reports--all using various strongly-typed datasets, etc. I think I found the problem, in that it holds the dataset information in the .rdlc file itself and expects ALL the fields to be there. Unless someone knows a way to override it -requiring- all fields to be there(?), I assume I'll have to parse the XML in the .rdlc file at runtime also and add in the XML for all the calculated fields I add as it adds them to the datatables themselves. Any better suggestions are appreciated though. =)

Thanks!

|||Okay, dynamically adding the columns worked for my immediate needs, but that soon passed. :-\ Is there ANY way at all to link different datatables in the same dataset together? I need to take a value from a field in the current scope (i.e. "FamilyID" in the dsMember_Entity dataset) and link it up with a field in another dataset ("FamilyName" in dsMember_Family where dsMember_Entity.FamilyID = dsMember_Family.FamilyID). There are a LOT of fields that will be worked this way, so are there any functions that would allow me to do this using calculated fields? If not, are there any suggestions how one might go about this?|||There are no built-in mechanisims in RS for joining multiple datasets together. Is it possible to join the entity and family table together in the SQL query, so that all fields are avaliable? If not, then you may want to look at writing a custom data extenstion that joins the data tables into one dataset. Or you may want to look into using custom code to return the appropriate value from one dataset given a value from another.

Let me know if you want more information about any of these topics.

Ian|||

Hi Ian,

The tables are joined in to one dataset. We're using strongly-typed datasets, and have all of the relationships already set up. Is there any way to use the existing relationships to get the values I'd like?

|||With the built-in data extensions, you can only access one table per dataset, so the tables need to be joined into one table based on the relationships already set up to be used in a data region. (This is without the use of custom lookup code and secondary datasets.) Did you join the tables on FamilyID, so each row would have the rows from the dsMember_Family table joined with the appropriate rows of the dsMember_Entity table? If so, the fields should be available in the same fields collection.

Also, how are you creating and accessing the strongly typed dataset?

Ian

|||

The dataset has multiple tables, as some of the results are many-to-one we can't just lop all the data onto one table. Foreign key relationships exist in that strongly-typed dataset, however.

We have an internal utility that creates the strongly-typed dataset from a schema it gets from stored procedures. I.e., a "GetEntityData" stored procedure will return several different resultsets--our schema generator checks out these results and writes a strongly-typed dataset based on it... then we just add them into our project and can do any changes we require.

The datasets are accessed in the WinForm's code; we want to use one form for reports instead of one form per report, so we just pass in the dataset we need directly to the form and it handles the binding.