Solved

TSQL Concatenation with uniqueidentifier type

Posted on 2008-10-08
5
1,854 Views
Last Modified: 2012-05-05
I'm trying to assemble an insert statement inside a stored procedure:

The value of @move_guid is equal to the output of newid()

The procedure can compile without errors, but when I try to run it, I get:

Arithmetic overflow error converting expression to data type nvarchar.

How do I concatenate a uniqueidentifier into a string?
DECLARE @cmd nvarchar(max)

set @cmd='select * from move_request where guid=''' + cast(@move_guid as nvarchar) + ''' and completed=1'

execute(@cmd)

Open in new window

0
Comment
Question by:PMH4514
  • 3
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22670122
don't use dynamic sql for that?


select * from move_request where guid= @move_guid and completed=1

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 22670134
in case you REALLY need dynamic sql, do it like this:
DECLARE @cmd nvarchar(max)
set @cmd='select * from move_request where guid= @guid and completed=1'
exec sp_Executesql @cmd, N'@guid uniqueidentifier' , @move_guid 

Open in new window

0
 

Author Comment

by:PMH4514
ID: 22670168
I do need to use dynamic SQL - the query string is more complex than that built based on various search filters. I omitted all of that for simplicity.

Can you explain how your 2nd comment is working?  is sp_Executesql a system stored procedure that can take typecasting details as input or something?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22670199
>Can you explain how your 2nd comment is working?  

yes. the first parameter of sp_executesql (which is a system procedure, so available in your sql server) is the sql statement, eventually having some @variables.

those needs to be defined in the second parameter: a nvarchar parameter holding all the parameter definititions. if you have more than 1 parameter to pass, see below example.

as from the third paramter, it's the values you want to pass, in the EXACT same order as they are defined in the second parameter.


DECLARE @cmd nvarchar(max)

set @cmd='select * from move_request where guid= @guid and completed= @b_completed '

exec sp_Executesql @cmd, N'@guid uniqueidentifier, @b_completed bit' , @move_guid , 1

Open in new window

0
 

Author Comment

by:PMH4514
ID: 22670264
Makes perfect sense. Thanks!
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now