Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 dynamic sql question

Author  Topic 

dbist
Yak Posting Veteran

52 Posts

Posted - 2008-05-06 : 15:36:50
why can't I do this?

declare @stamp nvarchar(25)
declare @sql nvarchar(25)
set @stamp = 'select current_timestamp'
set @sql = exec(@stamp)

what can I do to achieve what I need to do?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-06 : 15:42:41
Why are you using dynamic SQL?

declare @dt datetime
select @dt = current_timestamp

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

dbist
Yak Posting Veteran

52 Posts

Posted - 2008-05-06 : 16:00:52
I am trying to return the timestamp and use it in the following backup statement to dynamically append timestamp to backup file name. So I do need @sql to store the current time.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-06 : 16:04:55
You do not need to use dynamic SQL to append a timestamp to the filename in the backup command.

Check out my backup code to see:
http://weblogs.sqlteam.com/tarad/archive/2007/02/26/60120.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

sanoj_av
Posting Yak Master

118 Posts

Posted - 2008-05-09 : 06:52:45
Well.you need not use Dyanamic SQL for this. However you can use it as follows incase you want to use it for some other reason.

declare @stamp nvarchar(50)
declare @sql nvarchar(25)
Declare @Param nvarchar(25)
set @param= N'@nsql varchar(25) output'
set @stamp = 'select @nsql=current_timestamp'
exec sp_executesql @stamp,@param,@nsql=@sql output
select @sql


quote:
Originally posted by tkizer

You do not need to use dynamic SQL to append a timestamp to the filename in the backup command.

Check out my backup code to see:
http://weblogs.sqlteam.com/tarad/archive/2007/02/26/60120.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx

Go to Top of Page
   

- Advertisement -