Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Implicit conversion from datatype......


Implicit conversion from datatype......

Author
Message
Anchelin
Anchelin
SSC-Enthusiastic
SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)

Group: General Forum Members
Points: 128 Visits: 1091

Hi All

I get the following error:

Implicit conversion from data type sql_variant to nvarchar is not allowed. Use the CONVERT function to run this query.

My query is as follows:

DECLARE @count1 int

DECLARE @count2 int

DECLARE @ServerName varchar(300)

DECLARE @Query1 nvarchar(2000)

DECLARE @Query2 nvarchar(2000)

DECLARE @Query3 nvarchar(2000)

set @count1 =0

set @count2 =(Select max(ServerID) from Table where Status = 'Active')

While @count1 <= @count2

begin

set @count1=@count1 +1

set @ServerName =(Select distinct ServerName from Table where Status = 'Active' AND serverID = +@count1)

set @Query2 = 'insert into AnotherTable'

set @Query3 = @Query2 + ' SELECT * FROM OPENQUERY ('+@ServerName +','+''' SET FMTONLY OFF exec master.sp__Sumthing'')'

Print @Query3

exec (@Query3)

end

Can anyone of you perhaps help me with this one?

Thanks in advance

Anchelin


YB
YB
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 2

Doesn't seem to be a clear reason why you might be getting that error, but I would start by checking the data types of the value being assigned to @count2. If your @serverName value is an IP or has a space or something, you might wanna wrap it with "[ ]". Other thank that. I just tweaked the cod a little bit for readability.

DECLARE @count1 int

DECLARE @count2 int

DECLARE @ServerName varchar(300)

DECLARE @Query nvarchar(3000)

set @count1 =0

set @count2 =(Select max(ServerID) from Table where Status = 'Active')

While @count1 <= @count2

begin

set @count1=@count1 +1

set @ServerName =(Select distinct ServerName from Table where Status = 'Active' AND serverID = +@count1)

set @Query = 'insert into AnotherTable SELECT * FROM OPENQUERY (' + @ServerName + ', SET FMTONLY OFF exec master.sp__Sumthing)'

--Print @Query

exec (@Query)

end

Very interested in knowing the outcome of this issue. Good Luck


noeld
noeld
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6320 Visits: 2048

First When you use INSERT/SELECT you should especify the colum list on the insert table and on the select too.

That way you know exactly what goes where.

Secondly your source procedure is returning an sql_variant type which I could guess is coming from one of the serverproperty,databaseproperty(ex) or objectproperty built-in functions in SQL Server and the error *clearly* indicates that you have to convert it to the destination type explicitily.

Cheers,




* Noel
logicinside22
logicinside22
Old Hand
Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)

Group: General Forum Members
Points: 330 Visits: 1391
Hi
Are you able to solve this error ?I am working on it also . Let me know what steps you took to resolve this please.
thanks

Aim to inspire rather than to teach.
SQL Server DBA
DBA_Dom
DBA_Dom
Mr or Mrs. 500
Mr or Mrs. 500 (580 reputation)Mr or Mrs. 500 (580 reputation)Mr or Mrs. 500 (580 reputation)Mr or Mrs. 500 (580 reputation)Mr or Mrs. 500 (580 reputation)Mr or Mrs. 500 (580 reputation)Mr or Mrs. 500 (580 reputation)Mr or Mrs. 500 (580 reputation)

Group: General Forum Members
Points: 580 Visits: 81110
Can you post definitions for 'table' and 'another table'. My guess is that they have a SQL_VARIANT column and you are push it into a NVARCHAR variable without explictly converting it.

http://msdn.microsoft.com/en-us/library/ms187928.aspx
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47261 Visits: 44392
logicinside22 (4/13/2012)
Hi
Are you able to solve this error ?I am working on it also . Let me know what steps you took to resolve this please.
thanks


Also asked here: http://www.sqlservercentral.com/Forums/Topic1283379-391-1.aspx

Please don't post the same question in multiple places, it just results in people answering an already answered question.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Anchelin
Anchelin
SSC-Enthusiastic
SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)SSC-Enthusiastic (128 reputation)

Group: General Forum Members
Points: 128 Visits: 1091
Thanks noeld; this did the trick.... well 3 years ago :-D
Thanks again!

A
dmswebdev
dmswebdev
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 1
I know this is an old post but noeld's answer helped me too. I posted my solution over here: http://www.sqlservercentral.com/Forums/Topic1273528-1063-1.aspx?Update=1
if anyone is interested.
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