SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Error converting data type varchar to bigint


Error converting data type varchar to bigint

Author
Message
river1
river1
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4194 Visits: 1366
Hi,

I'm doing this query:

DECLARE @TESTE BIGINT
DECLARE @DB VARCHAR(100)

SET @DB ='SGCTCENTRAL'

SET @TESTE = ('SELECT MAX(CODIGO) FROM '+@DB+'.DBO.SINCRONISMO')

SELECT @TESTE

I get this message:

Msg 8114, Level 16, State 5, Line 8
Error converting data type varchar to bigint.

Can someone help, please.

Thank you
Luis Cazares
Luis Cazares
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39863 Visits: 19802
You're setting your variable to 'SELECT MAX(CODIGO) FROM SGCTCENTRAL.DBO.SINCRONISMO' and that is a string value. You need to use dynamic code by using sp_executesql (preferred method) or EXECUTE().
If your query is as simple as this, you don't need to use the @TESTE variable. You could execute the SELECT directly.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90154 Visits: 38944
river1 (3/28/2014)
Hi,

I'm doing this query:

DECLARE @TESTE BIGINT
DECLARE @DB VARCHAR(100)

SET @DB ='SGCTCENTRAL'

SET @TESTE = ('SELECT MAX(CODIGO) FROM '+@DB+'.DBO.SINCRONISMO')

SELECT @TESTE

I get this message:

Msg 8114, Level 16, State 5, Line 8
Error converting data type varchar to bigint.

Can someone help, please.

Thank you





declare @SQLCmd nvarchar(max),
@SQLParm nvarchar(max),
@TESTE bigint,
@DB nvarchar(128);

SET @DB = N'SGCTCENTRAL';
set @SQLCmd = N'SELECT @TESTOUT = MAX(CODIGO) FROM '+@DB+'.DBO.SINCRONISMO');
set @SQLParm = N'@TESTOUT bigint';

exec sp_executesql @SQLCmd, @SQLParm, N'@TESTOUT = @TESTE OUTPUT';
select @TESTE




Not sure if I did all of that correctly as I did it from memory and I have not tested in my sandbox database. At least it gives you an idea of how to proceed if it works.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search