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
 General SQL Server Forums
 New to SQL Server Programming
 Dynamic sql problem

Author  Topic 

Sean_B
Posting Yak Master

111 Posts

Posted - 2007-05-01 : 05:20:35
Hi,
Can anyone help.

I'm trying to populate a variable (@FileDate) in a stored procedure via a piece of dynamic sql (see below).

Unfortunately this doesn't seem to work. Is there a way around this ?

SET @SQL = 'select @FileDate = t1.FileDate ' + char(10)
SET @SQL = @SQL + 'FROM OPENROWSET (BULK' + char(10)
SET @SQL = @SQL + ' ''' + @DataFileFilePath + @FileName + ''',' + char(10)
SET @SQL = @SQL + ' LASTROW=1,' + char(10)
SET @SQL = @SQL + ' FORMATFILE=''' + @FormatFilePath + 'tempibesdate.xml'') AS t1' + char(10)


Sean

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-05-01 : 05:28:00
are you using exec() ? You should be using sp_executesql


KH

Go to Top of Page

Sean_B
Posting Yak Master

111 Posts

Posted - 2007-05-01 : 05:51:08
Yes, I was using exec, I've changed it to sp_executesql and it now works, thank you.

Sean
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-05-05 : 04:27:27
More on dynamic sql
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -