Return Stored Proc Results in CTE - sql-server

Is it possible for me to call a stored proc into a CTE. I have a login to our reporting DB that is only RO. I have write access to our UAT but would like to query live data.
So can I use a stored proc in a CTE?
with clientOwes as (
exec des_Batch_GetApplicationClientOwesList
)
select a.des_applicationnumber
from des_heapplicationset a
where a.des_heapplicationid in (select applicationid from clientowes)
result was: Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'exec'.

Answer adapted from dialogue in comments:
You can use a stored procedure to populate a table variable, which Read Only access does allow you to create. You won't need to use OpenRowSet to populate it either. Just do:
INSERT INTO #MyTableVariable
EXEC MyStoredProcedure
I do this in a lot of places myself where I need to treat Stored Proc results as a table that I can JOIN or UNION with other tables.

Related

How do I select from a linked temp table, into a local temp table?

I'm currently calling a stored procedure on a linked server. This stored procedure selects data into a temporary table on this linked server. I'm then attempting to select this data into a temporary table on the local server, so that I can manipulate the data and pull it into various tables.
If I manually run the stored procedure on the server, I'm able to run the second part of the query (--SELECT DATA FROM TEMP TABLE). However, although I'm able to successfully call the stored procedure via the first part of my query, when it gets to the second part, I get the below errors:
Msg 8180, Level 16, State 1, Line 66
Statement(s) could not be prepared.
Msg 208, Level 16, State 1, Line 66
Invalid object name '##Sales'.
Is there another method I could use here? SSIS isn't an option right now, the requirement is to code this via T-SQL.
--CALL STORED PROC FOR SALES DATA
DECLARE #RunSalesStoredProcSQL VARCHAR(1000);
SET #RunSalesStoredProcSQL = 'EXEC [SERVER\INSTANCE].[DATABASE].[dbo].[Extract_Sales_Data]';
EXEC (RunSalesStoredProcSQL) AT [SERVER\INSTANCE];
Print ‘Sales Procedure Executed';
--SELECT DATA FROM TEMP TABLE
SELECT *
INTO ##TempTable
FROM OPENQUERY([SERVER\INSTANCE], 'SELECT * FROM dbo.##Sales');
Print 'Data Selected';
You could return the data from linked server procedure as a simple select, not selecting it in a temp table, and then you can do the following on the local server:
INSERT INTO ##TempTable(ColNames)
EXECUTE [SERVER\INSTANCE].[DATABASE].[dbo].[Extract_Sales_Data]
--select data
SELECT * FROM ##TempTable

i have create a basic store procedure..when i m changing in this and execute it give me error that this name of SP already exit

This is my code
USE AccountSystemTraining
GO
CREATE PROCEDURE dbo.precetics
AS
SELECT * FROM Department where id=1
GO
error:
Msg 2714, Level 16, State 3, Procedure precetics, Line 4 There is
already an object named 'precetics' in the database.
If the procedure already exists and you need to change it then you may need to use ALTER instead of CREATE:
ALTER PROCEDURE dbo.precetics AS SELECT * FROM Department where id=1 GO
Note: From SQL Server 2016 you can do
CREATE OR ALTER PROCEDURE dbo.precetics AS SELECT * FROM Department where id=1 GO
which will always work without erroring regardless the procedure exists or not. In previous SQL server you can go around the issue by testing for existence first.

creating view and procedure for non existense table?

I have one query regards the creating a procedure and view.
Simple :
Create Procedure usp_demoXYZ
As
Begin
Select * from Table1 -- This table is not present in current Database
End
After execute the above query, query is executed successfully and created procedure.
But same things we apply in time of creating View.
Create View Vw_demoXYZ
As
Select * from Table1 -- This table is not present in current Database
after that I m running above query , sql server is showing error..
Msg 208, Level 16, State 1, Procedure Vw_demoXYZ, Line 4
Invalid object name 'Table1'
Stored procedures use Deferred Name Resolution while views does not do that. Deferred Name Resolution means when you create a stored proc it only checks if syntax of the statements are correct and not if the objects/tables it is referring to are there in database. That only occurs at runtime when you execute stored proc.
More details can be found here :
https://technet.microsoft.com/en-us/library/ms190686(v=sql.105).aspx

Incorrect Syntax: Create Procedure must be the only statement in the batch

This question has been asked before but it all involved using "go" which I am not in need of here, at least I believe so.
I am following this tut https://www.youtube.com/watch?v=-xMGwiV5A6o, near the 1:25 mark exactly. And his seems to execute while mine doesn't.
Select * From Snacks
Create Proc spGetSnackByID
#Id int
as
Begin
Select Id, Name, Location
from Snacks where Id = #Id
End
Here is the exact error, being highlighted with the "BEGIN" statement:
"Msg 111, Level 15, State 1, Procedure spGetSnackByID, Line 7
'CREATE/ALTER PROCEDURE' must be the first statement in a query batch."
If you want to keep the script as it is (select followed by a create procedure), you can construct the creation of the stored procedure in a NVARCHAR and EXECUTE it using sp_executesql. This way the CREATE statement is the first statement. Like this:
Select * From Snacks
EXECUTE sp_executesql N'
Create Proc spGetSnackByID
#Id int
as
Begin
Select Id, Name, Location
from Snacks where Id = #Id
End
';
Yes, SQL Server wants the create procedure as the first line. Just remark out the select above, it is not what you want anyway, because you have specified the fields in the stored procedure.

TSQL insert exec(#command)

I am wondering why below statement is not working:
insert into #temp (ip, ping) values ('ip', exec xp_cmdshell 'ping ip')
I would like to get resultset where I will have ip address in one column and ping from that server. Above query returns errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'exec'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
Thanks in advance for explanations.
You can't execute anything in a INSERT statement value list. You need to execute like this:
insert into #temp
execute xp_cmdshell 'ping ip'
[This assumes that the #temp table has a column structure matching the resultset of xp_cmdshell ]
You can also create an intermediate temp table to contain all the columns that the stored procedure returns and then insert just the ones you want into your final table.
Well, insert ... exec is notoriously inflexible. For one thing, it won't work with a values clause. Even more annoyingly, it does not support a column list. The columns in the table have to match the output of the stored procedure exactly.
The only way to use insert ... exec is:
insert TableName exec(...)
-- ^^^--> no column list!
Here's an example:
if exists (select * from tempdb.sys.tables where name like '#temp__%')
drop table #temp
create table #temp (output varchar(max))
insert into #temp execute xp_cmdshell 'ping pong'
select * from #temp

Resources