Execute the output of an SP - sql

I have a stored procedure 'GetInsertTrigger' which returns below Output.
I need to concat these lines and execute it again.
Also, I cannot insert this output into temp table.
create table #otable (line nvarchar(max))
insert into #otable (line)
exec GetInsertTrigger #TableName = 'cci_parms_mst'
Error msg is
Msg 8164, Level 16, State 1, Procedure GetInsertTrigger , Line 1266 [Batch Start Line 64]
An INSERT EXEC statement cannot be nested.
I cannot modify GetInsertTrigger as it is standard in our system.

Related

How to insert the result of a stored procedure when it has a parameter that doesn't let it's output be inserted into a temp table

I am using SQL Server and trying this out
insert into #temptable
exec [dbo].[sp_name]
--list
--of
--parameters
and I get an error
Msg 8164, Level 16, State 1, Procedure sp_name, Line 841 [Batch Start Line 0]
An INSERT EXEC statement cannot be nested.
I have declared the temp table to match the output of the stored procedure, too.
Is there any way to get around this?

Error executing stored procedure with uniqueidentifier input parameter

When I execute the stored procedure that has the first input parameter of type uniqueidentifier which is the primary key column in a table that inserts the values into, it is throwing the below error. I couldn't find whats wrong with this input or stored procedure.
EXEC #return_value = DbInsert
#id = EX642793-385D-604F-BE81-0000CD376836,
#name= N'000001_067_0xed642993385d604fbe810000cd376836.tif',
#image = 0x49492A00080000001300FE00040001000000020000000001040001000000C50900000101040001000000E90D0000020103000
SELECT 'Return Value' = #return_value
My stored procedure:
CREATE PROCEDURE DbInsert
#id UNIQUEIDENTIFIER,
#name VARCHAR(255),
#image IMAGE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO TbImage (Id, Name, Image )
VALUES (#id, #name, #image)
END
Error:
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near '-'.
Update
I tried to right click the stored procedure and execute, and it didn't ask me for quote and all, but the query I wrote above is what it shown as error. When I try with quote, it throw below error
Msg 8114, Level 16, State 1, Procedure DbInsert, Line 0 [Batch Start Line 2]
Error converting data type varchar to uniqueidentifier. (1 row(s) affected)
Update again
Using valid Uniqueidentifier by changing some numbers myself, however replaced X with some other char...still no luck.
EM662993-385D-604F-BE81-0000CD376838
There are 2 issues with your code:
You need to single quote uniqueidentifiers e.g. 'DE5AA552-0601-453C-AF21-9B285FA4E920'.
A guid/uniqueidentifier must contain only valid hexidecimal characters, and X is not a valid hexidecimal character and is therefore not valid within a uniqueidentifier so 'EX642793-385D-604F-BE81-0000CD376836' is not valid but 'EA642793-385D-604F-BE81-0000CD376836' is. You can use http://guid.us/Test/GUID to verify and generate guids.

Stored procedure for a login with user and password as parameter

create procedure createacc(
#loginnaam nvarchar(30),
#wachtwoord nvarchar(30))
AS
CREATE LOGIN #loginnaam
WITH PASSWORD = #wachtwoord
CREATE USER #loginnaam FOR LOGIN #loginnaam
ALTER ROLE db_datareader ADD #loginnaam
Go
This gives me syntax errors while it works fine outside of the Stored Procedure.
Msg 102, Level 15, State 1, Procedure createacc, Line 5
Incorrect syntax near '#loginnaam'.
Msg 319, Level 15, State 1, Procedure createacc, Line 6
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 156, Level 15, State 1, Procedure createacc, Line 8
Incorrect syntax near the keyword 'ADD'.
I would suggest using dynamic sql for this. Declare a nvarchar variable, put your statement inside it and execute that statement.
For example in your case:
DECLARE #statement NVARCHAR(MAX)
SET #statement = 'CREATE LOGIN ' + #loginnaam + ' WITH PASSWORD = '''+ #wachtwoord + ''' ;'
exec (#statement)
Above answer may be velnarable to SQL injection, it is concating SQL statement and then executing it.
Is there any way to pass password string as a parameter like example below
declare #query nvarchar(500)
declare #params nvarchar(500)
declare #passwordVal nvarchar(100)
set #passwordVal=N'Test#123'
set #query = 'Alter LOGIN [SqlUser] WITH password=#pass'
set #params = N'#pass NVARCHAR (100)';
EXECUTE sp_executesql #query,#params, #pass=#passwordVal
This query generates error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '#pass'.
Better alternative answer to parameter is here
I am trying to create a stored procedure to create a login and a database user?

create trigger using a stored procedure

I have a trigger and I want to kick it off from a stored procedure. I am using ms access and when i run the trigger from ms access it gives me an error msg (ODBC). I think I can't create triggers using ms access. This is my trigger:
IF EXISTS
(SELECT name
FROM sys.objects
WHERE name = 'UpdateComments' AND type = 'TR')
DROP TRIGGER tblEmailHdr_abenit01.UpdateComments;
GO
CREATE TRIGGER UpdateComments
ON tblEmailHdr_abenit01
AFTER Update
AS
IF ( UPDATE (Comments) ) BEGIN Update ttblEmailHdr_abenit01
Set UpdateComm = GetDate()
END;
GO
This is how I have been trying to create the trigger from the stored procedure but I get the following error msg's when I try to create the sproc:
Sproc:
CREATE PROCEDURE dbo.SP_AS_tblEmailHdr_Trig (#UserID as varchar(10))
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
--SET NOCOUNT ON;
-- Insert statements for procedure here
Declare #UserTable Varchar(50)
Declare #UserTable2 Varchar(50)
Set #UserTable = 'tblEmailHdr_' + #UserID ;
Set #UserTable2 = 'tblEmailHdr_' + #UserID + '.UpdateComments' ;
IF EXISTS
(SELECT name
FROM sys.objects
WHERE name = 'UpdateComments' AND type = 'TR') DROP TRIGGER #UserTable2
GO
CREATE TRIGGER UpdateComments
ON #UserTable
AFTER UPDATE
AS
IF ( UPDATE (Comments) )
BEGIN
--RAISERROR (50009, 16, 10)
Update #UserTable
Set UpdatedComm = GetDate()
END
GO
END
GO
error msg i get:
Msg 102, Level 15, State 1, Procedure SP_AS_tblEmailHdr_Trig, Line 23
Incorrect syntax near '#UserTable2'.
Msg 102, Level 15, State 1, Procedure UpdateComments, Line 2
Incorrect syntax near '#UserTable'.
Msg 1087, Level 15, State 2, Procedure UpdateComments, Line 8
Must declare the table variable "#UserTable".
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'END'.
create procedure pro (parameters)
as
begin
declare #trigs nvarchar(max)
declare #trip nvarchar(max)
set #trigs='
create trigger tri on dbo.employee
for insert
as
select * from inserted
go'
set #trip='drop trigger tri'
EXEC sp_executeSQL #trigs
insert into employee values(parameters)
EXEC sp_executeSQL #trip
end
exec pro param
eg:
exec pro 80,'aaa','AAS',25000,'2013-02-01','iT'
remove all the GO statements from inside the procedure.

how to pass tablename as parameter in sql server

hello frnds i need to pass a table name as parameter to stored procedure
CREATE PROCEDURE six #tablename nvarchar
AS
SELECT * FROM + #tablename
Go
exec six Entry_sixsigma_mag
it gives error like
Msg 102, Level 15, State 1, Procedure six, Line 3
Incorrect syntax near '+'.
Msg 208, Level 16, State 1, Procedure six, Line 3
Invalid object name '#sixsigma'.
Try something like
CREATE PROCEDURE six #tablename nvarchar(100)
AS
EXEC('SELECT * FROM ' + #tablename)
Go
exec six Entry_sixsigma_mag
Have a look at EXECUTE (Transact-SQL)
But you should also have a look at
SQL Injection
Dynamic SQL & SQL injection
before using this blindly.