18

I want to declare a VARCHAR variable in MSSQL that can hold this

set @RaiseErrorMessage = 
              ('ErrorNumber='+(cast((select ERROR_NUMBER()) as varchar(100)))+
              ,ErrorSeverity='+ (cast((select ERROR_SEVERITY()) as varchar(100)))+
             ',ErrorState='+(cast((select ERROR_STATE()) as varchar(100)))+                                                             
                ',ErrorLine='+(cast((select ERROR_LINE()) as varchar(100)))+
                ,ErrorMessage='+(cast((select ERROR_MESSAGE()) as varchar(100))))

How should the declaration look for an variable like this? I tried

 declare @RaiseErrorMessage  varchar

but it didn't help.

1 Answer 1

30

You'll need to declare the length of the variable:

DECLARE @RaiseErrorMessage VARCHAR(500)
SET @RaiseErrorMessage=' ...... '
1
  • 8
    To add to this, if you're using SQL Server 2008 and above, this can all be set in one line, if desired: DECLARE @RaiseErrorMessage VARCHAR(500) = 'ErrorNumber=[...]'
    – Tom
    Apr 2, 2014 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.