|
Below I am trying to update a table with the variable @addflag however instead of updating the table with the required set variable it is inserting nulls. Everything else in the statement works apart from the update section. Can anyone help explain how I can get the table to update?
Thanks in advance.
(comments are locked)
|
|
You need to put single quotes around addFlag in your string concatenation, and as they are themselves within a string you will have to double them up. Hopefully this: Thanks. That runs the query but inserts 'X' for ALL the records: CLIENT ADDFLAG NAME Is my IF THEN ELSE outwith the scope of the update??
Dec 04 '09 at 06:53 AM
David 2 1
Ooops sorry for the formatting.
Dec 04 '09 at 06:54 AM
David 2 1
I thought that would probably be the case when I looked at the code, but wasn't sure. Your "IF @client" logic is OUTSIDE the actual cursor so the final ELSE will ALWAYS be true : ), so all that code would need to be part of the dynamic SQL I'm afraid ...
Dec 04 '09 at 08:39 AM
Kristen ♦
Thanks. Do you have an example you could provide? I just seem to be going round in circles with incorerect syntax errors. :((
Dec 04 '09 at 09:23 AM
David 2 1
(comments are locked)
|
|
I think you are going to need something like:
SELECT @tabload = 'INSERT INTO ' + @tabname + 'CODE (client,name)
select client,title from ' + @tabname + 'TBL'
SELECT @tabup = 'UPDATE ' + @tabname + 'CODE
SET addflag =CASE @client WHEN 12692 THEN ''C'' WHEN 1600031 THEN ''A'' '
...
these things are notoriously difficult to debug, so if you can find a different solution I think that would be best. I would put a PRINT or SELECT statement in place of the INSERT / UPDATE, initially, so you get some OUTPUT of what the INSERT / UPDATE would actually do, and then you can debug that until it is doing what you want, then chagne it to the INSERT / UPDATE commands. Thanks. I haven't yet considered CASE because the code I have given above is trimmd down from what the final will be as there's going to be a lot of: IF .... AND ... AND ... SET variable ELSE .... Can CASE handle such queries?
Dec 04 '09 at 09:44 AM
David 2 1
(comments are locked)
|
|
Refer this to know how single quotes work in SQL Server http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx
(comments are locked)
|

