Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare VARCHAR variables in MSSQL

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.

like image 473
Vignesh Subramanian Avatar asked Apr 02 '14 16:04

Vignesh Subramanian


People also ask

How do I DECLARE varchar?

Example - Declare a variable For example: DECLARE @techonthenet VARCHAR(50); This DECLARE statement example would declare a variable called @techonthenet that is a VARCHAR datatype, with a length of 50 characters.

How do you set varchar in SQL?

varchar [ ( n | max ) ] Variable-size string data. Use n to define the string size in bytes and can be a value from 1 through 8,000 or use max to indicate a column constraint size up to a maximum storage of 2^31-1 bytes (2 GB).

How DECLARE variable in SQL Server?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

What is varchar in SQL with example?

VARCHAR is a variable length string data type, so it holds only the characters you assign to it. VARCHAR takes up 1 byte per character, + 2 bytes to hold length information. For example, if you set a VARCHAR(100) data type = 'Jen', then it would take up 3 bytes (for J, E, and N) plus 2 bytes, or 5 bytes in all.


1 Answers

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

DECLARE @RaiseErrorMessage VARCHAR(500)
SET @RaiseErrorMessage=' ...... '
like image 131
dsolimano Avatar answered Sep 18 '22 18:09

dsolimano