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.
| Author |
Topic |
|
reddymade
Posting Yak Master
165 Posts |
Posted - 2008-10-31 : 14:17:39
|
I am getting an error exactly on this line: incorrect syntax: WHEN 'Spanish' THEN ' + char(39) + @LangText + Char(39) + ',' + char(39) + @LangDescription + Char(39) + 'CREATE PROCEDURE [dbo].[USP_NewPicklist] (@pickid int, @language nvarchar(50), @fieldlabelkey nvarchar(200), @englishdescription nvarchar(100), @englishtext nvarchar(200), @LangText nvarchar(100), @LangDescription nvarchar(200), @UserName nvarchar(50))ASDECLARE @ErrorCode intDECLARE @SQLStatement nvarchar(2000)DECLARE @SQLSelect nvarchar(1000)DECLARE @SQLFields nvarchar(500)DECLARE @MaxPickId Int SET @MaxPickId = (SELECT MAX(pickID) FROM TAB_ccsNetPickLists WHERE fieldlabelkey= @fieldlabelkey) IF @MaxPickId is null SET @pickid = '1' ELSE BEGIN SET @pickid = @MaxPickId + 1 ENDSET NOCOUNT ONSelect @SQLFields = CASE @language WHEN 'Spanish' THEN ' Spanishtext, Spanishdescription,' WHEN 'Catalan' THEN 'Catalantext, Catalandescription,' ELSE 'Spanishtext, Spanishdescription,'ENDSELECT @SQLSelect = CASE @language WHEN 'Spanish' THEN ' + char(39) + @LangText + Char(39) + ',' + char(39) + @LangDescription + Char(39) + ' WHEN 'Catalan' THEN ' + char(39) + @LangText + Char(39) +','+ char(39) + @LangDescription + Char(39) + ' ELSE ' + char(39) + @LangText + Char(39) + ','+ char(39) + @LangDescription + Char(39) + 'ENDSET @SQLStatement = 'INSERT INTO TAB_ccsNetPickLists(pickid,fieldlabelkey,UpdatedBy,englishtext,englishdescription,' + char(39)+ @SQLFields + char(39)+ ') Values('+ convert(nvarchar(50), @pickid) + ',' + char(39)+ @fieldlabelkey +char(39)+ ','+char(39)+ @UserName +char(39)+ ',' +char(39)+ @englishtext +char(39)+ ', ' + char(39)+ @englishdescription +char(39)+ ',' + char(39)+ @SQLSelect + char(39)+ ')' print (@SQLStatement)---EXEC (@SQLStatement)SELECT @ErrorCode = @@ErrorSET NOCOUNT OFFRETURN @ErrorCodeThank you very much for the help. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-31 : 14:30:32
|
| what's the need of dynamic sql here? why cant you simply use main statement itself. whats the purpose of all these varchars variables and EXEC() ? |
 |
|
|
reddymade
Posting Yak Master
165 Posts |
Posted - 2008-10-31 : 14:37:27
|
Hello Visakh,How can i write a direct insert without dynamic, when i have two different languages.At any point it will be only one language, both the languages does'nt go in one shot.if the language is Spanish, then these fields: Spanishtext, Spanishdescription,if it is catalan then:'Catalantext, Catalandescriptionquote: Originally posted by visakh16 what's the need of dynamic sql here? why cant you simply use main statement itself. whats the purpose of all these varchars variables and EXEC() ?
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-31 : 14:45:14
|
| use CASE WHEN... |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-10-31 : 14:46:05
|
The issue is at the comma's, is this what you want?Declare @Language varchar(20),@SqlSelect varchar(2000)set @Language = 'Spanish'SELECT @SQLSelect = CASE @language WHEN 'Spanish' THEN ' + char(39) + @LangText + Char(39) + '','' + char(39) + @LangDescription + Char(39) + ' WHEN 'Catalan' THEN ' + char(39) + @LangText + Char(39) + '' , '' + char(39) + @LangDescription + Char(39) + ' ELSE ' + char(39) + @LangText + Char(39) + '','' + char(39) + @LangDescription + Char(39) + 'ENDselect @SqlSelect Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-31 : 14:53:57
|
isnt the below insert enough for you?INSERT INTO TAB_ccsNetPickLists(pickid,fieldlabelkey,UpdatedBy,englishtext,englishdescription,Spanishtext, Spanishdescription,Catalantext, Catalandescription)SELECT convert(nvarchar(50), @pickid) , @fieldlabelkey,@UserName,@englishtext,@englishdescription,CASE @languageWHEN 'Spanish' THEN @LangTextELSE NULLEND,CASE @languageWHEN 'Spanish' THEN @LangDescriptionELSE NULLEND,CASE @languageWHEN 'Catalan' THEN @LangTextELSE NULLEND,CASE @languageWHEN 'Catalan' THEN @LangDescriptionELSE NULLEND |
 |
|
|
reddymade
Posting Yak Master
165 Posts |
Posted - 2008-10-31 : 15:03:06
|
| I am trying to use this one instead of dynamic sql query:Insert into TAB_ddlfields(pickid,fieldlabelkey,updatedby, Case @language When 'Spanish' Then spanishtext, When 'Catalan' Then CatalanText, Else SpanishText, End Case @language When 'Spanish' Then spanishdescription, When 'Catalan' Then catalandescription, Else spanishdescription, Endenglishtext,englishdescription) Values(@pickid,@fieldlabelkey,@UserName,@LangText,@LangDescription,@englishtext,@englishdescription)I am still getting error at the Case: Incorrect syntax.Thank you all very much |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2008-10-31 : 15:13:44
|
| visakh gave you the answer why are you so adamant on using case in the list of columns? that wont work. |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-10-31 : 15:39:05
|
quote: Originally posted by rohitkumar visakh gave you the answer why are you so adamant on using case in the list of columns? that wont work.
?? You can use it in a dynamic scenerio. I believe Visakh is correct and you can achieve this without needing to, but here is an example of it being used dynamically.Declare @SqlSelect varchar(2000), @SqlSTatement varchar(2000),@SqlFields Varchar(2000)Declare @pickid int, @language nvarchar(50), @fieldlabelkey nvarchar(200), @englishdescription nvarchar(100), @englishtext nvarchar(200), @LangText nvarchar(100), @LangDescription nvarchar(200), @UserName nvarchar(50)set @Language = 'Spanish'set @PickID = 1set @FieldLabelKey = 'hello test'set @EnglishDescription = 'THIS IS MY ENGLISH DESCRIPTION'set @EnglishText = 'THIS IS MY ENGLISH TEXT'set @LangTEXT = 'THIS IS MY LANG TEXT'set @LangDESCRIPTION = 'THIS IS LANG DESCRIPTION'set @UserName = 'Vinnie'Select @SQLFields = CASE @language WHEN 'Spanish' THEN ' Spanishtext, Spanishdescription' WHEN 'Catalan' THEN 'Catalantext, Catalandescription' ELSE 'Spanishtext, Spanishdescription,'ENDSELECT @SQLSelect = CASE @language WHEN 'Spanish' THEN char(39) + @LangText + Char(39) + ',' + char(39) + @LangDescription + Char(39) WHEN 'Catalan' THEN char(39) + @LangText + Char(39) + ',' + char(39) + @LangDescription + Char(39) ELSE char(39) + @LangText + Char(39) + ',' + char(39) + @LangDescription + Char(39) END--select @SqlSelectSET @SQLStatement = 'INSERT INTO TAB_ccsNetPickLists(pickid,fieldlabelkey,UpdatedBy,englishtext,englishdescription,' + @SQLFields + ') Values('+ convert(nvarchar(50), @pickid) + ',' + char(39)+ @fieldlabelkey +char(39)+ ','+char(39)+ @UserName +char(39)+ ',' +char(39)+ @englishtext +char(39)+ ', ' + char(39)+ @englishdescription +char(39)+ ',' + @SQLSelect + ')' Exec( @SqlStatement) Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-01 : 02:25:18
|
| Vinnie. isnt it better to avoid using dynamic sql for scenarios like this? If it was case where your table is changing dynamically or something its fine. but here the reqmnt is just to place column values only if variable value corresponds to particular languages which can be achieved easily using CASE...WHEN... then why use dynamic sql? |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-11-03 : 10:43:42
|
100% agree that dynamic should be avoided unless needed, and in a properly developed database there should really not be a reason that you would need a case statment to be used to select the colums. However, I just was correcting the syntax in reddymade's post to show him how to use his method to achieve what he wanted. If I was starting from scratch and all I wanted to accomplish was to insert into a table with certain values in certain fields, I would simply define all variables and pass them accordingly into the procedure, avoiding a need for a case statement or dynamic. Unfortuantly I do not know his front end, or any other details, so I can not determine if using the non-dynamic method shows him a correct solution to his problem. Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-03 : 11:03:08
|
ok. just checked ..thanks for clarification.. |
 |
|
|
|
|
|
|
|