You are here: Re: Variables in SQL-script SQL2005 « MsSQL Server « IT news, forums, messages
Re: Variables in SQL-script SQL2005

Posted by Brian Sprogψ on 01/22/08 08:46

Hi Dan,

I modified the script to look like this, and it worked like a charm :-)

USE [master]
GO
DECLARE @brugernavn as char(50)
DECLARE @password as char(50)
DECLARE @SqlStatement nvarchar(MAX);

set @brugernavn = 'testbruger'
set @password = N'testpassword'

SET @SqlStatement =
N'CREATE LOGIN ' +
QUOTENAME(@brugernavn) +
N' WITH PASSWORD = ' +
QUOTENAME(@password, '''') +
N', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english],
CHECK_EXPIRATION=ON, CHECK_POLICY=ON ';

EXECUTE (@SqlStatement);

use [testbase]

SET @SqlStatement =
N'CREATE USER ' +
QUOTENAME(@brugernavn) +
N' FOR LOGIN ' +
QUOTENAME(@brugernavn)

EXECUTE (@SqlStatement);
GO

Thanks a lot for your help.

Best regards,

Brian

Dan Guzman skrev:
>> How do I get the script to do what I want it to ?
>
> Not all DCL commands accept variables (see BOL syntax) so you'll need to
> use dynamic SQL. For example:
>
> DECLARE @SqlStatement nvarchar(MAX);
>
> SET @SqlStatement =
> N'CREATE LOGIN ' +
> QUOTENAME(@brugernavn) +
> N' WITH PASSWORD = ' +
> QUOTENAME(@password, '''');
>
> EXECUTE (@SqlStatement);
>

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация