tsql : can't select into <table> from proc
I'm running sql server 2008 and I'm trying to run the query
OPEN CUR_InvHeader
FETCH NEXT FROM CUR_InvHeader INTO
@CH_iBilling_Log_RecID
WHILE (@@FETCH_STATUS = 0) BEGIN --Loop for Cur_InvHeader
select * into tarInvoiceDetail_201101 FROM OPENROWSET('SQLNCLI', 'Server=(local);Trusted_Connection=yes;',
'EXEC _sp_cwm5_GetInvoiceDetail @CH_iBilling_Log_RecID')
I get the error
OLE DB provider "SQLNCLI10" for linked server "(null)" returned message "Deferred prepare could not be completed.".
I've run sp_configure 'ad hoc..', 1 reconfigure with override to ensure i can run the select against the open开发者_JAVA技巧recordset
any ideas what i might be doing wrong? thanks in advance
The first thing that's wrong is that your "EXEC _sp_cwm5_GetInvoiceDetail @CH_iBilling_Log_RecID" statement will be evaluated as a constant, rather than pass the value of @CH_iBilling_Log_RecID through to OPENROWSET. You'd have to replace the whole "SELECT * INTO..." into a string variable and run sp_executesql on it.
The second thing wrong is that your "select into" statement will create tarInvoiceDetail_201101 every time, as INTO creates a new table when run. So if your cursor covers more than one row you'll get an error on the second pass.
Also, what does "Server=(local)" represent here? Are you trying to use OPENROWSET against your local copy of SQL Server while connected to a remote server? Or are you trying to use OPENROWSET to get around the limitation that you can't directly use a stored procedure as the source for an INSERT?
精彩评论