Showing posts with label logical. Show all posts
Showing posts with label logical. 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 9, 2012

ALL and empty set

SQL Server 2000 SP4. Hoping to find a logical explanation for a certain
behavior.

Consider this script:

USE pubs
GO

IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = 'CA')
PRINT 'TRUE'
ELSE
PRINT 'FALSE'

This, as expected, prints FALSE, since not all authors in CA are under
contract. Now, if the script is changed as follows:

USE pubs
GO

IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = '')
PRINT 'TRUE'
ELSE
PRINT 'FALSE'

then the result is TRUE. In other words, the expression evaluates to TRUE
when the select statement produces an empty set, which doesn't make sense
to me. Even more interesting, the expression

NULL = ALL (SELECT contract FROM dbo.authors WHERE state = '')

still evaluates to TRUE (ANSI_NULLS is ON).

Can anyone explain these results? Is this the expected behavior in the SQL
standard, or something that is specific to SQL Server? Thanks.

--
remove a 9 to reply by emailDimitri Furman wrote:

Quote:

Originally Posted by

Consider this script:
>
USE pubs
GO
>
IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = 'CA')
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
>
This, as expected, prints FALSE, since not all authors in CA are under
contract. Now, if the script is changed as follows:
>
USE pubs
GO
>
IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = '')
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
>
then the result is TRUE. In other words, the expression evaluates to TRUE
when the select statement produces an empty set, which doesn't make sense
to me. Even more interesting, the expression
>
NULL = ALL (SELECT contract FROM dbo.authors WHERE state = '')
>
still evaluates to TRUE (ANSI_NULLS is ON).
>
Can anyone explain these results? Is this the expected behavior in the SQL
standard, or something that is specific to SQL Server? Thanks.


It's vacuously true because there are no elements in the set to
produce a non-equality.|||On 29.09.2006 05:24, Dimitri Furman wrote:

Quote:

Originally Posted by

SQL Server 2000 SP4. Hoping to find a logical explanation for a certain
behavior.
>
Consider this script:
>
USE pubs
GO
>
IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = 'CA')
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
>
This, as expected, prints FALSE, since not all authors in CA are under
contract. Now, if the script is changed as follows:
>
USE pubs
GO
>
IF 1 = ALL (SELECT contract FROM dbo.authors WHERE state = '')
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
>
then the result is TRUE. In other words, the expression evaluates to TRUE
when the select statement produces an empty set, which doesn't make sense
to me. Even more interesting, the expression
>
NULL = ALL (SELECT contract FROM dbo.authors WHERE state = '')
>
still evaluates to TRUE (ANSI_NULLS is ON).
>
Can anyone explain these results? Is this the expected behavior in the SQL
standard, or something that is specific to SQL Server? Thanks.


I think that's general boolean logic. If you evaluate AND over a set of
items you start with TRUE and go until you reach the first FALSE. In
Ruby:

Quote:

Originally Posted by

Quote:

Originally Posted by

>def and_all(items)
>items.each {|i| return false unless i}
>true
>end


=nil

Quote:

Originally Posted by

Quote:

Originally Posted by

>and_all [true, true, true]


=true

Quote:

Originally Posted by

Quote:

Originally Posted by

>and_all [true, true]


=true

Quote:

Originally Posted by

Quote:

Originally Posted by

>and_all [true]


=true

Quote:

Originally Posted by

Quote:

Originally Posted by

>and_all []


=true

Quote:

Originally Posted by

Quote:

Originally Posted by

>and_all [true, false, true]


=false

Quote:

Originally Posted by

Quote:

Originally Posted by

>and_all [true, true, false]


=false

Kind regards

robert|||Dimitri,

I do not think it is a good idea to use ALL clause in production ever,
because it is not intuitive to understand.
The mainstream approach is to use IN and/or EXISTS and/or JOIN.
Also note that queries using mainstream features have a better chance
to get a decent execution plan.

--------
Alex Kuznetsov
http://sqlserver-tips.blogspot.com/
http://sqlserver-puzzles.blogspot.com/|||On Fri, 29 Sep 2006 03:24:09 -0000, Dimitri Furman wrote:

(snip)

Quote:

Originally Posted by

the expression evaluates to TRUE
>when the select statement produces an empty set, which doesn't make sense
>to me.


Hi Dimitri,

Hmm, it actuallly makes perfect sense to me. This is a bit like me
bragging that I've slain every single dragon that has ever set fooot in
my back yard. And that I've rescued every princess that has been locked
away in a high tower during my life time.

Of course, I've never fought a dragon or rescued a princess - but since
no dragon has ever set foot in my back yard (see! they are THAT afraid
of me <g>) and no princess has been locked away in a high tower during
my life time, the statements above are still true.

Quote:

Originally Posted by

Even more interesting, the expression
>
>NULL = ALL (SELECT contract FROM dbo.authors WHERE state = '')


This is an interesting, since you can defend two answers. The simple
version is that any comparison involving NULL should result in unknown.
The other, almost equally simple, version is that if the subquery
produces an empty set, it doesn;t matter what value is on the left-hand
side; we can be sure that it'll be equal to all values in the empty set.
So we don't care if the value for the left-hand side is supplied or
missing, since we can evaluate anyhow.

Since both definitions have their merit, we'll have to resort to
consulting the documentation. Books Online says this about ALL:

"Returns TRUE when the comparison specified is TRUE for all pairs
(scalar_expression, x), when x is a value in the single-column set;
otherwise returns FALSE."

Since there are no values in the set, there are no pairs - just as there
are no dragons in my back yard.

To double-check, I consulted the description of ALL in the ISO standard
SQL-2003. That one puts it even more bluntly, since it excplicitly
mentions that case that the subquery returns an empty set:

"Case:
"a) If T is empty or if the implied <comparison predicateis True for
every row RT in T, then R <comp op<allT is True."

--
Hugo Kornelis, SQL Server MVP|||Hi Hugo,

On Sep 29 2006, 05:24 pm, Hugo Kornelis
<hugo@.perFact.REMOVETHIS.info.INVALIDwrote in
news:ap2rh2540dnp6ssibojofp8fcdv73cn4mp@.4ax.com:

Quote:

Originally Posted by

On Fri, 29 Sep 2006 03:24:09 -0000, Dimitri Furman wrote:
>

Quote:

Originally Posted by

>Even more interesting, the expression
>>
>>NULL = ALL (SELECT contract FROM dbo.authors WHERE state = '')


>
This is an interesting, since you can defend two answers. The simple
version is that any comparison involving NULL should result in
unknown. The other, almost equally simple, version is that if the
subquery produces an empty set, it doesn;t matter what value is on the
left-hand side; we can be sure that it'll be equal to all values in
the empty set. So we don't care if the value for the left-hand side is
supplied or missing, since we can evaluate anyhow.


All right, this is not too surprising considering how "consistently" NULLs
are treated in SQL. I guess we can write it off as another example. Now
when anyone says that a NULL is not equal to anything, I'll have a
valid objection! :)

Quote:

Originally Posted by

Since both definitions have their merit, we'll have to resort to
consulting the documentation. Books Online says this about ALL:
>
"Returns TRUE when the comparison specified is TRUE for all pairs
(scalar_expression, x), when x is a value in the single-column set;
otherwise returns FALSE."


This is what bothers me. In the case of an empty set there are no pairs,
and the comparisons cannot be either TRUE or FALSE - there can be no
comparisons in the first place - so according to the above, this should
fall under "otherwise" and return FALSE.

Quote:

Originally Posted by

To double-check, I consulted the description of ALL in the ISO
standard SQL-2003. That one puts it even more bluntly, since it
excplicitly mentions that case that the subquery returns an empty set:
>
"Case:
"a) If T is empty or if the implied <comparison predicateis True for
every row RT in T, then R <comp op<allT is True."


Well, this settles it. Thanks for digging this up - that's really the
answer I was looking for.

--
remove a 9 to reply by email