SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL SELECT * FROM OPENROWSET with Variable
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Imelda
Starting Member

3 Posts

Posted - 05/17/2013 :  07:15:00  Show Profile  Reply with Quote
I am trying to pass a variable into a SELECT statement in OPENROWSET but I keep getting an error `DECLARE @dDateTIME DATE

SET @dDateTIME = (SELECT SalesDate FROM dbo.SalesDate)

INSERT INTO dbo.tblSales SELECT * FROM OPENROWSET('MSDASQL', 'dsn=mydsn;uid=myid;pwd=mypwd;', 'SELECT ID, TranDate, ProductID, CostValue, SalesValue, QtySold, FROM tblSales WHERE TranDate='' + @dDateTIME + ''')`

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 05/17/2013 :  07:19:19  Show Profile  Reply with Quote
you should be using sp_executesql for this

see

http://msdn.microsoft.com/en-us/library/ms175170(v=sql.105).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2224 Posts

Posted - 05/17/2013 :  07:21:34  Show Profile  Reply with Quote
Check this?

TranDate=''' + @dDateTIME + ''')'

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000