Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server openquery() with stored proc not returning data

Tags:

sql

sql-server

I have a stored proc that returns some data (just 1 row of dates). When I just call this proc with exec I get the returned data and all is good.

I want to make this accessible via a view so I use openquery() to do that. However when I do that I get the error message:

Cannot process the object "exec GetDates". The OLE DB provider "SQLNCLI11" for linked server "LOCAL" indicates that either the object has no columns or the current user does not have permissions on that object.

I've noticed it's the insert statement on the temp table. If I comment that out then I don't get this error. So I assume it's the permission side of that error message. It's a temp table, how can I not have permission to insert into that when I can insert into any real table in the database?

ALTER PROCEDURE [dbo].[GetDates]
AS
    DECLARE @capacityTable TABLE (dt datetime)
    DECLARE @dt datetime
    DECLARE curDates CURSOR FOR SELECT dt from calendar;

    -- loop over all dates we care about
    OPEN curDates;
    FETCH NEXT FROM curDates INTO @dt;
    WHILE @@FETCH_STATUS = 0
    BEGIN
        INSERT INTO @capacityTable (dt)
        VALUES(@dt)

        FETCH NEXT FROM curDates INTO @dt;
    END;
    CLOSE curDates;
    DEALLOCATE curDates;

    -- return the temp table results
    SELECT dt from @capacityTable;
RETURN

alter view GetDates_vw
as
select * from openquery(LOCAL, 'exec GetDates')
like image 949
user441521 Avatar asked Sep 14 '25 11:09

user441521


1 Answers

It looks like before the EXEC in the openquery() function if you place SET NOCOUNT ON; it works. Not sure why but it does.

like image 137
user441521 Avatar answered Sep 16 '25 02:09

user441521