Question : Set SQL Results to a Variable

Hi Expert

I find out if I can set a print result in sql to a variable and use the variable for a insert statement. I have my code below
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
Set @ErrorMessage = print 'Serial Number ' + ' ' + @varSerialNumber + ' ' + ' Already Exist' 
				  'Asset Type'	   + ' ' + @varType 
				  'Asset Make'	   + ' ' + @varMake		 
				  'Asset Model'    + ' ' + @varModel
								   + ' ' + 'Please Check Upload File'
			insert into invalid (record,Datetime)
			Values (@ErrorMessage,getdate())

Answer : Set SQL Results to a Variable

do not use PRINT there:


Set @ErrorMessage = 'Serial Number ' + ' ' + @varSerialNumber + ' ' + ' Already Exist'
                          'Asset Type'         + ' ' + @varType
                          'Asset Make'         + ' ' + @varMake            
                          'Asset Model'    + ' ' + @varModel
                                                   + ' ' + 'Please Check Upload File'
PRINT @ErrorMessage
                  insert into invalid (record,Datetime)
                  Values (@ErrorMessage,getdate())

note: if any of the variables is not varchar, you might need to apply a explicit cast, like:



Set @ErrorMessage = 'Serial Number ' + ' ' + CAST( @varSerialNumber as VARCHAR(100)) + ' ' + ' Already Exist'
                          'Asset Type'         + ' ' + @varType
                          'Asset Make'         + ' ' + @varMake            
                          'Asset Model'    + ' ' + @varModel
                                                   + ' ' + 'Please Check Upload File'
                  insert into invalid (record,Datetime)
                  Values (@ErrorMessage,getdate())
Random Solutions  
 
programming4us programming4us