Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Conversion Error

Author  Topic 

binto
Yak Posting Veteran

59 Posts

Posted - 2010-04-16 : 05:38:38
Hi All,

CREATE TABLE #SalesInvoicesDetails
(
DocNo VARCHAR(20),
ItmNo VARCHAR(20),
Description VARCHAR(50),
Qty DECIMAL(38,20),
QtyBase DECIMAL(38,20),
UntOfMeasureCd VARCHAR(20),
UnitPr DECIMAL(38,20),
Amt DECIMAL(38,20)
)
DECLARE @tblVar VARCHAR(3000)
DECLARE @Qry VARCHAR(8000)

SET @tblVar='INSERT INTO #SalesInvoicesDetails
SELECT [Document No_],[No_],[Description],[Quantity],[Quantity (Base)],[Unit of Measure Code],
[Unit Price],[Line Amount] FROM ['+@CompanyName+'$Sales Invoice Line] WHERE [Document No_]='+@InvoiceNo+''



PRINT @tblVar
EXEC(@tblVar)
SELECT * FROM #SalesInvoicesDetails


Document No_ is 'int' and '@InvoiceNo' is also 'int'
and getting error

Msg 245, Level 16, State 1, Procedure FormNo8BNew, Line 33
Conversion failed when converting the varchar value 'INSERT INTO #SalesInvoicesDetails
SELECT [Document No_],[No_],[Description],[Quantity],[Quantity (Base)],[Unit of Measure Code],
[Unit Price],[Line Amount] FROM [Jomsons Test$Sales Invoice Line] WHERE [Document No_]=' to data type int.


Please help me to write this dynamic query

Thanks & Regards
Binto Thomas

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-04-16 : 05:52:53
+convert(varchar(20),@InvoiceNo) instead of +@InvoiceNo


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -