Author |
Topic |
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-13 : 11:16:15
|
hii have the following code for replacing a string.SET ANSI_NULLS OFFSET QUOTED_IDENTIFIER OFF-- Declare varibles DECLARE@SQLStmt NVARCHAR(MAX),@FieldName NVARCHAR(50),@StringReplacement NVARCHAR(200),@StringToFind NVARCHAR(200),@maxPK INT,@pk INT,@Quote NVARCHAR(1),@WorkingNamingTable NVARCHAR(256),@WorkingDataloadFile3 NVARCHAR(256)SELECT @WorkingNamingTable = ?SELECT @WorkingDataloadFile3 = ?--Make sure the temp table has been dropped--Only for use when testing in SQL mgmt studio--BEGIN-- DROP TABLE #StringReplace--ENDSET @Quote = '"'-- Create temp table SELECT @SQLStmt = 'Select PK_IDINTO #StringReplaceFROM @WorkingNamingTable -- Get the max ID in the table -- table is new each time so do not need to worry about -- ID not starting at 1Select @maxPK = MAX(PK_ID) FROM #StringReplaceSET @pk = 1-- Loop thru the table WHILE @pk <= @maxPKBEGIN -- Preform replacement (note we are replacing in the whole field not just the end) SELECT @FieldName = Field from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) SELECT @StringReplacement = StringReplacement from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) SELECT @StringToFind = StringToFind from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) UPDATE ' + @WorkingDataloadFile3 + ' SET ' + @FieldName + ' = ' + ' " " + ' + @FieldName + ' + " " FROM '+ @WorkingDataloadFile3 +' WHERE ' + @FieldName + ' IS NOT NULL UPDATE ' + @WorkingDataloadFile3 + ' SET ' + @FieldName + ' = REPLACE(' + @FieldName + ',' + @Quote + @StringToFind + @Quote + ',' + @Quote + @StringReplacement + @Quote + ') FROM ' + @WorkingDataloadFile3 + ' WHERE ' + @FieldName + ' IS NOT NULL Select @pk = @pk + 1 ENDSET @pk = 1-- Loop thru the table WHILE @pk <= @maxPKBEGIN UPDATE '+ @WorkingDataloadFile3 +' SET ' + @FieldName + ' = LTRIM(RTRIM(' + @FieldName + ')) FROM @WorkingDataloadFile3 WHERE ' + @FieldName + ' IS NOT NULL Select @pk = @pk + 1END'EXECUTE(@SQLStmt)SET ANSI_NULLS ONSET QUOTED_IDENTIFIER ONGO it does not seem to update the field. can anone see anything wrong with code |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-04-13 : 12:13:42
|
Add PRINT statements before the UPDATE for each of the variables. You might have a NULL issue.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 04:26:20
|
i have put PRINT (@StringToFind) PRINT (@StringReplacement)before the updates but its not showing up anything. any ideas why? |
|
|
Kristen
Test
22859 Posts |
Posted - 2015-04-14 : 04:52:18
|
You are not displaying enough information to debug it, instead ofPRINT (@StringToFind)at the least I would use:PRINT ''@pk='' + CONVERT(varchar(20), @pk) + '', @FieldName='' + COALESCE(@FieldName, ''[NULL]'') This code:Select PK_IDINTO #StringReplaceFROM @WorkingNamingTable Also beforeEXECUTE(@SQLStmt)putPRINT '@SQLStmt=' + COALESCE(@SQLStmt, '[NULL]')EXECUTE(@SQLStmt) and try executing whatever SQL is displayed (I would put BEGIN TRANSACTION and ROLLBACK around it, so that it doesn't update anything and you can just test that it runs OK / the syntax has no errors etc.)Last point:inside your SQLStmt string: I don't think?? that an @TemporaryTable inside the SQL String as I don't think the @WorkingNamingTable in outer code is in scope, is it?the |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 05:11:53
|
i suppose my bigest problem is this is not my code and im not used to t sql.i have two tables WorkingNamingTable and WorkingDataloadFile3in the WorkingNamingTable i have the followingStringTOFind Stringreplacment fieldN Ap securitname/dev securitynameand i want NAP TO BE UPDATE UNDER THE COULMN SECURITYNAME in theWorkingDataloadFile3.would there be a problem replacing Nap with blank.? |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 05:19:59
|
and there is not id the matches from 1 table to the other |
|
|
Kristen
Test
22859 Posts |
Posted - 2015-04-14 : 06:02:00
|
quote: Originally posted by rjhe22 would there be a problem replacing Nap with blank.?
No, unless there is a trigger that rolls back the transaction if that column is blank. Seems very unlikely.You need to use PRINT statements in your dynamically generated SQL string to display what actual SQL is being executed.You can then try that SQL manually to see what happens. I expect you will get a syntax error, you will also get a message indicating what the error is, so you can fix it in your working-code, run it again until it works, and then retro-fit the changes you made back into the original code that generates the dynamic SQL.If you don't understand the error you get you can post the SQL and error message here and I am sure someone will help.At the very least make this change:PRINT '@SQLStmt=' + COALESCE(@SQLStmt, '[NULL]')EXECUTE(@SQLStmt) post the SQL which is displayed, and also execute that SQL like this:BEGIN TRANSACTIONGO[i]your SQL here/i]ROLLBACK and also post any error message you get from that. |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 06:12:19
|
I put ththis line PRINT '@SQLStmt=' + COALESCE(@SQLStmt, '[NULL]')in beforeEXECUTE(@SQLStmt)at the end of the script and i got this @SQLStmt=[NULL] |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 06:56:41
|
cif i leave this two lines inSELECT @WorkingNamingTable = ?SELECT @WorkingDataloadFile3 = ?i get the following error messageMsg 102, Level 15, State 1, Line 16Incorrect syntax near '?'.Msg 102, Level 15, State 1, Line 17Incorrect syntax near '?'. |
|
|
Kristen
Test
22859 Posts |
Posted - 2015-04-14 : 07:04:13
|
OK, well that's your first problem then and explains why nothing happens when you run the routine.Something in the code that constructs the @SQLStmt is NULL. If you concatenate something which is NULL to something else then the result is always NULL.You could use COALESCE to find them, e.g. change:...SET ' + @FieldName + ' = ' + ' " " + ' + @FieldName + ' + " " FROM '+ @WorkingDataloadFile3 +'... to...SET ' + COALESCE(@FieldName, '@FieldName_NULL') + ' = ' + ' " " + ' + COALESCE(@FieldName, '@FieldName_NULL') + ' + " " FROM '+ COALESCE(@WorkingDataloadFile3, '@WorkingDataloadFile3_NULL') +'... This will raise syntax errors if anything attempts to run it, but you should be able to see what the problem is in the SQL that is output by the PRINT statement.You need to do this for every string concatenation (which you think MIGHT be NULL). You can start with the most obvious, and keep repeating the replaces until the output from the PRINT statement stops being "[NULL]" |
|
|
Kristen
Test
22859 Posts |
Posted - 2015-04-14 : 07:06:31
|
quote: Originally posted by rjhe22 cif i leave this two lines inSELECT @WorkingNamingTable = ?SELECT @WorkingDataloadFile3 = ?i get the following error messageMsg 102, Level 15, State 1, Line 16Incorrect syntax near '?'.Msg 102, Level 15, State 1, Line 17Incorrect syntax near '?'.
I assumed you were replacing those with some valid parameters. The syntaxSELECT @WorkingNamingTable = ? doesn't mean anything - its just an indication that you need to provided a value and as I indicated beforeINTO #StringReplaceFROM @WorkingNamingTable is not valid. What you probably mean isINTO #StringReplaceFROM ' + @WorkingNamingTable + ' if that is what you meant you need to replace the other instances, and if there is a risk that @WorkingNamingTable might be NULL you need to use COALESCE to give you a dummy value when it is null, for debugging purposes. |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 10:16:20
|
I CANT GET IT TO WORK NO MATTER WHAT I DO.WOULD ANYONE NO A CLEANER OR BETTER WAY TO DO THIS.ANY CODE WOULD BE GREAT |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 11:37:26
|
IM JUST STRIPPING OUT THE SELECT AND UPDATE PART SELECT @FieldName = Field from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) SELECT @StringReplacement = StringReplacment from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) SELECT @StringToFind = StringToFind from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) UPDATE ' + @WorkingDataloadFile3 + ' SET ' + @FieldName + ' = ' + ' " " + ' + @FieldName + ' + " " FROM '+ @WorkingDataloadFile3 +' --WHERE ' + @FieldName + ' IS NOT NULL how does it no what field in @WorkingDataloadFile3 table to update.we name the slect fields from the @WorkingNamingTable.am i reading it wrong? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2015-04-14 : 12:15:08
|
Also use QUOTENAME function just in case @FieldName contains illegal characters. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 12:35:03
|
DONT THINK THATS THE PROBLEM BUT I WILL TRY THAT AS WELL. |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 12:49:55
|
CAN I TEST IT LIKE THISSET @FieldName ='SecurityName'SET @StringReplacement ='BL'SET @StringToFind ='N AP'SET @Quote = '"'SET @maxPK = '1'SET @pk = '1'AND IF SO WHERE CAN I PUT THAT CODE WHEN I PUT IT IN AFTER THE DECLARE I GET THE FLOWING ERRORSMust declare the scalar variable "@pk".Msg 137, Level 15, State 1, Line 41Must declare the scalar variable "@pk".Msg 137, Level 15, State 2, Line 44Must declare the scalar variable "@pk".Msg 1087, Level 15, State 2, Line 48Must declare the table variable "@WorkingDataloadFile3".Msg 137, Level 15, State 2, Line 51Must declare the scalar variable "@pk".EVEN THOUGH THEY ALL ARE DELCARED |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-04-14 : 12:58:19
|
Show us the entire code again that is now showing the syntax errors.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-14 : 13:02:05
|
[code]SET ANSI_NULLS OFFSET QUOTED_IDENTIFIER OFF-- Declare varibles DECLARE@SQLStmt NVARCHAR(MAX),@FieldName NVARCHAR(50),@StringReplacement NVARCHAR(200),@StringToFind NVARCHAR(200),@maxPK INT,@pk INT,@Quote NVARCHAR(1),@WorkingNamingTable NVARCHAR(256),@WorkingDataloadFile3 NVARCHAR(256)SELECT @WorkingNamingTable = 'dbo.SSCLXWorkingNamingTable'SELECT @WorkingDataloadFile3 = 'dbo.SSCLXWorkingDataloadFile3'SET @FieldName ='SecurityName'SET @StringReplacement ='BL'SET @StringToFind ='N AP'SET @Quote = '"'SET @maxPK = '1'SET @pk = '1'--Make sure the temp table has been dropped--Only for use when testing in SQL mgmt studio--BEGIN-- DROP TABLE #StringReplace--END-- Create temp table SELECT @SQLStmt = 'Select PK_IDINTO #StringReplaceFROM @WorkingNamingTable -- Get the max ID in the table -- table is new each time so do not need to worry about -- ID not starting at 1Select @maxPK = MAX(PK_ID) FROM #StringReplaceSET @pk = 1-- Loop thru the table WHILE @pk <= @maxPKBEGIN -- Preform replacement (note we are replacing in the whole field not just the end) --SELECT @FieldName = Field from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) --SELECT @StringReplacement = StringReplacement from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) --SELECT @StringToFind = StringToFind from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) UPDATE ' + @WorkingDataloadFile3 + ' SET ' + @FieldName + ' = ' + ' " " + ' + @FieldName + ' + " " FROM '+ @WorkingDataloadFile3 +' WHERE ' + @FieldName + ' IS NOT NULL UPDATE ' + @WorkingDataloadFile3 + ' SET ' + @FieldName + ' = REPLACE(' + @FieldName + ',' + @Quote + @StringToFind + @Quote + ',' + @Quote + @StringReplacement + @Quote + ') FROM ' + @WorkingDataloadFile3 + ' WHERE ' + @FieldName + ' IS NOT NULL Select @pk = @pk + 1 ENDSET @pk = 1-- Loop thru the table WHILE @pk <= @maxPKBEGIN UPDATE '+ @WorkingDataloadFile3 +' SET ' + @FieldName + ' = LTRIM(RTRIM(' + @FieldName + ')) FROM @WorkingDataloadFile3 WHERE ' + @FieldName + ' IS NOT NULL Select @pk = @pk + 1END'EXECUTE(@SQLStmt)SET ANSI_NULLS ONSET QUOTED_IDENTIFIER ONGO[/code] |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2015-04-14 : 13:54:26
|
The errors are because you've got a missing or extra single quote. The issue starts at line 33: SELECT @SQLStmt. You've got a single quote but no ending quote.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
Kristen
Test
22859 Posts |
Posted - 2015-04-14 : 20:27:23
|
You've got no PRINT statements in that code?And no COALESCE to change any NULLs that accidentally get into any of the variables into something that will raise an error but at least enable you to see where the problem is.You haven't sorted out:Select PK_IDINTO #StringReplaceFROM @WorkingNamingTableas per my earlier message |
|
|
rjhe22
Constraint Violating Yak Guru
283 Posts |
Posted - 2015-04-15 : 04:30:12
|
HiI hAVE PUT THE PRINT STATEMENT IN AND THE COALESCE BUT ALL IT DOES IT PRINT @SQLStmt=[NULL]IF I CHANGE THE NULL IN THE PRINT PART TO TEST THEN IT PRINTS@SQLStmt=[TEST]THIS IS MY UPDATED CODE IM NOT SURE IF I DID THE COALESCE RIGHTSET ANSI_NULLS OFFSET QUOTED_IDENTIFIER OFFBEGIN TRANSACTIONGO-- Declare varibles DECLARE@SQLStmt NVARCHAR(MAX),@FieldName NVARCHAR(50),@StringReplacement NVARCHAR(200),@StringToFind NVARCHAR(200),@maxPK INT,@pk INT,@Quote NVARCHAR(1),@WorkingNamingTable NVARCHAR(256),@WorkingDataloadFile3 NVARCHAR(256)SELECT @WorkingNamingTable = "dbo.SSCLXWorkingNamingTable"SELECT @WorkingDataloadFile3 = "dbo.SSCLXWorkingDataloadFile3"--Make sure the temp table has been dropped--Only for use when testing in SQL mgmt studio--BEGIN-- DROP TABLE #StringReplace--ENDSET @Quote = '"'-- Create temp table SELECT @SQLStmt = 'Select PK_IDINTO #StringReplaceFROM ' + @WorkingDataloadFile3 + '-- Get the max ID in the table -- table is new each time so do not need to worry about -- ID not starting at 1Select @maxPK = MAX(PK_ID) FROM #StringReplaceSET @pk = 1-- Loop thru the table WHILE @pk <= @maxPKBEGIN -- Preform replacement (note we are replacing in the whole field not just the end) SELECT @FieldName = Field from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) SELECT @StringReplacement = StringReplacement from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) SELECT @StringToFind = StringToFind from @WorkingNamingTable WHERE PK_ID = (Select PK_ID From #StringReplace Where PK_ID = @pk) UPDATE ' + @WorkingDataloadFile3 + ' --SET ' + @FieldName + ' = ' + ' " " + ' + @FieldName + ' + " " FROM '+ @WorkingDataloadFile3 +' SET ' + COALESCE(@FieldName, '@FieldName_NULL') + ' = ' + ' " " + ' + COALESCE(@FieldName, '@FieldName_NULL') + ' + " " FROM '+ COALESCE(@WorkingDataloadFile3, '@WorkingDataloadFile3_NULL') +' WHERE ' + @FieldName + ' IS NOT NULL UPDATE ' + @WorkingDataloadFile3 + ' SET ' + COALESCE(@FieldName, '@FieldName_NULL') + ' = REPLACE(' + COALESCE(@FieldName, '@FieldName_NULL') + ',' + @Quote + COALESCE(@StringToFind, '@StringToFind_NULL') + @Quote + ',' + @Quote + COALESCE(@StringReplacement, '@StringReplacement_NULL') + @Quote + ')) FROM ' + @WorkingDataloadFile3 + ' WHERE ' + @FieldName + ' IS NOT NULL Select @pk = @pk + 1 ENDSET @pk = 1-- Loop thru the table WHILE @pk <= @maxPKBEGIN UPDATE '+ @WorkingDataloadFile3 +' SET ' + COALESCE(@FieldName, '@FieldName_NULL') + ' = LTRIM(RTRIM(' + COALESCE(@FieldName, '@FieldName_NULL') + ')) FROM @WorkingDataloadFile3 WHERE ' + @FieldName + ' IS NOT NULL Select @pk = @pk + 1END'PRINT '@SQLStmt=' + COALESCE(@SQLStmt, '[NULL]')EXECUTE(@SQLStmt)ROLLBACK |
|
|
Next Page
|