Click here to Skip to main content
14,422,174 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to concat this two variables in to another..pls refer code

What I have tried:

-- Get primary key fields select for insert       
SELECT @PKFieldSelect = COALESCE(@PKFieldSelect+'+','') 
       + '''' + COLUMN_NAME +''''
       FROM    INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
               INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
       WHERE   pk.TABLE_NAME = @TableName
       AND     CONSTRAINT_TYPE = 'PRIMARY KEY'
       AND     c.TABLE_NAME = pk.TABLE_NAME
       AND     c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME  
       
select @PKValueSelect = coalesce(@PKValueSelect+'+','') + 'convert(varchar(100), coalesce(i.' + COLUMN_NAME + ',d.' + COLUMN_NAME + '))'
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
where pk.TABLE_NAME = @TableName
and CONSTRAINT_TYPE = 'PRIMARY KEY'
and c.TABLE_NAME = pk.TABLE_NAME
and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME


SET @PKSelect = 'select ' + @PKFieldSelect +  '+'',''+'  + @PKValueSelect + 'from'+ @TableName

or
like this
SET @PKSelect = @PKFieldSelect +   @PKValueSelect
Posted
Updated 15-Mar-16 7:48am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

A word of advice - instead of just asking the question, actually TRY stuff.
You will learn more by experimenting that just asking questions.

If you would have done that then you would realise that the first option produces
select 'EmployeeID'+','+convert(varchar(100), coalesce(i.EmployeeID,d.EmployeeID))fromEmployees
and the second produces
'EmployeeID'convert(varchar(100), coalesce(i.EmployeeID,d.EmployeeID))
(or whatever the appropriate bits and pieces are for the table you chose - I used the Employees table from the Northwind sample database from MSDN).

Whichever format of results you are after then the use the option that produces it.
   
Comments
Member 11363211 14-Mar-16 6:03am
   
Can u give me from above example which i have coded...Thanks
CHill60 14-Mar-16 9:48am
   
Give you what? I've told you what each of the queries produces but only you know which of the two formats is the one you want.
What are you expected results - give an example
Member 11363211 15-Mar-16 0:08am
   
Sir My expected result is two concat two variables in to another variable pls see above coded
example :SET @PKSelect = @PKFieldSelect + @PKValueSelect -- its not working just eg:
and i need to add single quotes for String
thanks
CHill60 15-Mar-16 6:42am
   
"its not working" does not help anyone to solve your issue.
Both of your attempts
1) SET @PKSelect = 'select ' + @PKFieldSelect + '+'',''+' + @PKValueSelect + 'from'+ @TableName
and
2) SET @PKSelect = @PKFieldSelect + @PKValueSelect
work.
Both those statements concatenate strings using the + operator
You already have the single quotes added to @PKFieldSelect e.g. '''' + COLUMN_NAME +''''
So give us a clue what the problem is, please!
I'm trying to help but you are not giving enough information (and having a solution voted down I'm still trying to get help is not encouraging me to spend more time on you)
Rate this:
Please Sign up or sign in to vote.

Solution 2

select CONCAT('EmployeeID',coalesce(i.EmployeeID,d.EmployeeID) )from Employees
   
Comments
CHill60 15-Mar-16 13:03pm
   
It would have been better to use the OP's variables as this query produces errors:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "i.EmployeeID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "d.EmployeeID" could not be bound.
Member 11363211 16-Mar-16 8:04am
   
thank u so much sir ..i have got it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100