Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic query quote problem.

Status
Not open for further replies.

simian101

MIS
Nov 16, 2007
113
US
Need some help with single and double quotes.

I am trying to build a dynamic query but I can't get the quotes right around the date.

declare @mydate datetime
set @mydate=getdate()

declare @comp
set @comp='1st'

--Version 1
exec('UPDATE [MyDatebase].[dbo].[' + @comp + ' table]' +
' SET [BillingDate] = ' + char(39) +
CONVERT(VARCHAR(10), @mydate, 101) + char(39))

--Version 2
exec('UPDATE [MyDatebase].[dbo].[' + @comp + ' table]' +
' SET [BillingDate] = CONVERT(VARCHAR(10), @mydate, 101)))


This is what the final query should look like

UPDATE [MyDatabase].[dbo].[1st table] SET [BillingDate] = '04/26/2009'


Thanks

Simi
 
When you do something like this, debug it first. Instead of trying to execute it first, use the PRINT statement to see if the string is a valid SQL statement.
Code:
DECLARE @Sql VARCHAR(1000)

SELECT @Sql = 'UPDATE [MyDatebase].[dbo].[' + @comp + ' table]' +
' SET [BillingDate] = ' + char(39) +
CONVERT(VARCHAR(10), @mydate, 101) + char(39))

PRINT @Sql

Then, execute the variable instead of the string literal.
 
I tried that but I get....

Incorrect syntax near ')'.

Thanks

Simi


 
DECLARE @Sql VARCHAR(1000)

SELECT @Sql = 'UPDATE [MyDatebase].[dbo].[' + @comp + ' table]' +
' SET [BillingDate] = ' + char(39) +
CONVERT(VARCHAR(10), @mydate, 101) + char(39)

PRINT @Sql

There was a typo - extra ) at the end.

 
If simian101 is Chadwick Kaufman, I'll freak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top