?
Solved

Passing the value from an select statement

Posted on 2007-08-08
6
Medium Priority
?
285 Views
Last Modified: 2008-06-20
I wrote the below script to restore a particular database via stored procedure. What I would like to do is have the results from set @path_Data =  N'select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Data_1''' to be displayed to the variable @r. So when the script is run the variable @r will have the results from the select statement. My syntax is wrong can you help me correct it? And how do I declare the @r variable?

Below is my code.

declare @dbname nvarchar(30)
declare @loc nvarchar(400)
set @loc ='\\ykcm1s1\y$\fromYKCMOS7\Plandata_<DATETIME yyyymmdd>.sqb'

declare @s nvarchar(4000)
set @dbname = 'qtstcmo3'


declare @path_Data nvarchar (400)
set @path_Data =  N'select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Data_1'''

declare @path_Data_1 varchar (400)
set @path_Data_1 = ('select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Data_1''')

declare @path_Data_2 varchar (400)
set @path_Data_2 = ('select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Data_2''')

declare @path_Data_3 varchar (400)
set @path_Data_3 = ('select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Data_3''')

declare @path_Data_4 varchar (400)
set @path_Data_4 = ('select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Data_4''')

declare @path_Log varchar (400)
set @path_Log = ('select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Log''')

--restore reporting database
set @s = 'master..sqlbackup N''-SQL "RESTORE DATABASE ['+ @dbname + ']'
set @s = @s + 'FROM DISK = '''''+ @loc +''''''
set @s = @s + 'WITH  MOVE ''''kings_plus_reference_Data'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_1'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_2'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_3'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_4'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Log'''' TO N'''+  @r + ', REPLACE"'''

print @s
0
Comment
Question by:redouard
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19655284
>declare @path_Log varchar (400)
>set @path_Log = ('select filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = ''kings_plus_reference_Log''')

you need sp_executesql to make that working.
here is one transcript:

declare @path_Log varchar (400)
declare @sql nvarchar(4000)
set @sql = 'select @res = filename from' +space(1)+ @dbname + '.dbo.sysfiles where name = @name '
exec sp_executesql @sql, '@res varchar(400) OUTPUT, @name sysname ', @path_log OUTPUT, 'kings_plus_reference_Log'

note that I made also the logical name a parameter, although not needed.
it just avoids the hassles of the quotes....

0
 

Author Comment

by:redouard
ID: 19655472
now it only show me the results of the select statement when I run the query.  Now how can I get that result and have it equal @r?

--restore reporting database
set @s = 'master..sqlbackup N''-SQL "RESTORE DATABASE ['+ @dbname + ']'
set @s = @s + 'FROM DISK = '''''+ @loc +''''''
set @s = @s + 'WITH  MOVE ''''kings_plus_reference_Data'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_1'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_2'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_3'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_4'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Log'''' TO N'''+  @r + ', REPLACE"'''
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19655604
did you see that I defined 2 variables?
the @sql is the one that contains the sql, while the @path_Log will get the value during the call using sp_executesql
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:redouard
ID: 19655839
the final output should resemble the following:

master..sqlbackup N'-SQL "RESTORE DATABASE [qtstcmo8]
FROM DISK = ''\\ykcm1s1\y$\fromYKCMOS7\Plandata_<DATETIME yyyymmdd>.sqb''
WITH  MOVE ''kings_plus_reference_Data'' TO ''E:\data\qtstcmo8.MDF'',
MOVE ''kings_plus_reference_Data_1'' TO ''E:\data\qtstcmo8_1.MDF'',
MOVE ''kings_plus_reference_Data_2'' TO ''E:\data\qtstcmo8_2.MDF'',
MOVE ''kings_plus_reference_Data_3'' TO ''E:\data\qtstcmo8_3.MDF'',
MOVE ''kings_plus_reference_Data_4'' TO ''E:\data\qtstcmo8_4.MDF'',
MOVE ''kings_plus_reference_Log'' TO ''F:\log\qtstcmo8_Log.LDF'', REPLACE"'

Instead its only giving me  E:\data\qtstcmo3.MDF
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 19655920
ase
set @s = 'master..sqlbackup N''-SQL "RESTORE DATABASE ['+ @dbname + ']'
set @s = @s + 'FROM DISK = '''''+ @loc +''''''
set @s = @s + 'WITH  MOVE ''''kings_plus_reference_Data'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_1'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_2'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_3'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Data_4'''' TO N'''+  @r + ','
set @s = @s + 'MOVE ''''kings_plus_reference_Log'''' TO N'''+  @r + ', REPLACE"'''

actually, tha tis the problem, as @r is nowhere initilized, you actually want to use the other variables @path_Data, @path_Data_1 etc...
0
 

Author Comment

by:redouard
ID: 19656811
I've made the necessary changes to the script and still no luck. I've tried everything I could.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question