TSQL Concatenation with uniqueidentifier type

Posted on 2008-10-08
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'

Open in new window

Question by:PMH4514
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 143

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

LVL 143

Accepted Solution

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


Author Comment

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?
LVL 143

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


Author Comment

ID: 22670264
Makes perfect sense. Thanks!

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

726 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