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.
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.
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).
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.
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.
You'll need to declare the length of the variable:
DECLARE @RaiseErrorMessage VARCHAR(500)
SET @RaiseErrorMessage=' ...... '
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With