Solved

TSQL Concatenation with uniqueidentifier type

Posted on 2008-10-08
5
1,902 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

803 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