Solved

TSQL Concatenation with uniqueidentifier type

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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
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.

919 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

18 Experts available now in Live!

Get 1:1 Help Now