sql server 2008 update table - sql

I have stored procedure that insert data to table
on this procedure after inserting data to the table i want to update data in this table by execute another procedure
but the data not updated.
just when i exec the updateTable procedure after the first procedure it works, but i need to exec the updateTable procedure from the first procedure!!!
this is the first procedure:
CREATE PROCEDURE a
AS
BEGIN
insert into tbl
select * from tempTable
exec updateTable
end
the procedure that update the table look like this:
ALTER procedure [dbo].[updateTable]
AS
BEGIN
update tbl
set tbl.name= users.name
from dbo.tbl tbl
inner join
dbo.users users
on users.id=tbl.id
END
what can i do????

Related

Save execute procedure results into a var in SQL Server 2008

I have a procedure that executes another procedure and I need to save the results into a variable.
How can I do that?
ALTER PROCEDURE sp_GetDetailsByUserId
#userId int
AS
BEGIN
SELECT
usr.FirstName, usr.LastName, usr.UserName, usr.Email
FROM
[User] usr
WHERE
usr.UserID = #userId
EXEC sp_GenerateRandomPass #userId // this result need to be inside a var
END
I am a beginner and need help.
Thank you.
you can declare a Table or you can use a Temp Table:
ALTER PROCEDURE sp_GetDetailsByUserId
#userId int
AS
BEGIN
SELECT usr.FirstName, usr.LastName, usr.UserName, usr.Email
FROM [User] usr
WHERE usr.UserID = #userId
declare #tbl table (the columns should be compatible with the result columns)
insert into #tbl
exec sp_GenerateRandomPass #userId // this result need to be inside a var
END
with temptable you can do:
ALTER PROCEDURE sp_GetDetailsByUserId
#userId int
AS
BEGIN
SELECT usr.FirstName, usr.LastName, usr.UserName, usr.Email
FROM [User] usr
WHERE usr.UserID = #userId
create table #tempTable(the columns should be compatible with the result columns)
insert into #tempTable
exec sp_GenerateRandomPass #userId // this result need to be inside a var
END
Can you change the procedure? Having output parameters would probably be best, assuming the select always returns just one row.
exec sp_GenerateRandomPass #userId, #password output
output parameters work this way:
ALTER PROCEDURE sp_GenerateRandomPass
#userId int,
#password varchar (100) output
AS
BEGIN
-- Magic happens
set #password = 'xxxx'
END
Other option is to change GenerateRandomPass to be a scalar function, you can assign value from it to a variable directly in your procedure
Also, please do not prefix your procedures with "sp_", it's intended for built in procedures.
You need to modify sp_GenerateRandomPass , Store result of query within this stored procedure to temptable. Find more about temptable.
Temp table will be accessible among stored procedures. So you can use that in sp_GetDetailsByUserId

SQL Server 2008 R2: Return result from stored procedure and insert into temp table

I have a table testing with some data:
Table:
select * from testing;
cola colb
---------
A B
C D
C X
S T
S Q
Stored procedure:
CREATE PROC spTesting
#TableName nvarchar(max)
AS
DECLARE #sql nvarchar(max)
SET #sql = 'SELECT * from '+ #TableName + ''
EXEC(#sql)
GO
Executing stored procedure:
execute spTesting 'testing'
I will get the result:
cola colb
---------
A B
C D
C X
S T
S Q
Note: After executing the stored procedure, I want to insert results into temptable which I don't want to declare with the structure before.
Like:
select * into temptable from execute spTesting 'testing'
There is this simple syntax of inserting into a table/temptable, a result set of a procedure
INSERT INTO TempTable
EXEC MyProc
But the gotcha with this approach is that TempTable should already exist before you can do the above.
When calling a stored procedure and inserting its result set into a table you cannot create a table on the fly like you can do with a select statement select * INTO temptabe From tablename.
You will need to create this table first and then Insert into it from your stored procedure.
Now since some tables can have 50 columns and finding their DDL and creating table before you can execute the procedure there is a simple way around something like ....
-- before you execute the procedure
SELECT * INTO TempTable FROM TargetTable WHERE 1 =2
-- this will create an empty table with matching schema of your TargetTable
-- Now execute the procedure
INSERT INTO TempTable
EXECUTE spTesting 'TargetTable'

How to pass a table variable from one stored procedure to another

My scenario is like this,
CREATE PROCEDURE SP_1
AS
BEGIN
INSERT INTO #tmpTable(ID, Value)
VALUES(1, 1), (2, 2)
END
GO
CREATE PROCEDURE SP_2
AS
BEGIN
CREATE TABLE #tmpTable(ID INT, Value INT)
EXEC SP_1
SELECT * FROM #tmpTable
DROP TABLE #tmpTable
END
GO
EXEC SP_2
GO
DROP PROCEDURE SP_1
DROP PROCEDURE SP_2
I want to replace this # table with a TABLE VARIABLE (# Table).
I tried to passed table variable as parameter to SP_1 but I should pass table variable as read only parameter. Since it's read only I can't insert into the table variable inside SP_1. Is there any other way I can accomplish this?
It's not gonna work this way. you should encapsulate your query within a string. once done you can change the table names as you like. and then, execute using EXEC sp_executesql.
i've previously answered a similar question which can be found below:
SQL: How to make table name in stored procedure dynamic
here's the example i've provided
declare #sql nvarchar(max)
declare #TableName nvarchar(max)
set #TableName = 'mytable'
set #sql = 'Select * from ' + #TableName
Exec sp_executesql #sql
This worked for me(Sql server 2012), although I did not completely understand what you are trying to achieve:
CREATE PROCEDURE SP_1
AS
BEGIN
Create table #tmpTable (ID INT, Value INT)
INSERT INTO #tmpTable(ID, Value)
VALUES(1, 1), (2, 2)
Select ID,Value from #tmpTable
END
GO
CREATE PROCEDURE SP_2
AS
BEGIN
Declare #tmpTable table (ID INT, Value INT)
Insert into #tmpTable
EXEC SP_1
SELECT * FROM #tmpTable
END
GO
EXEC SP_2
GO
DROP PROCEDURE SP_1
DROP PROCEDURE SP_2
I prefer passing data around as [xml]. It's easier to work with for me. You could build your record set similar to the code below and pass [xml] parameters between your procedures, then parse it out as shown.
declare #record_list [xml] = (select *
from [sys].[objects]
for xml path(N'record'), root(N'record_list'));
select #record_list;
select t.c.value(N'(./schema_id/text())[1]', N'[sysname]') as [schema_id]
, t.c.value(N'(./name/text())[1]', N'[sysname]') as [name]
, t.c.value(N'(./object_id/text())[1]', N'[sysname]') as [object_id]
from #record_list.nodes(N'/record_list/record') as t(c);

Insert into temp table from exec call stored procedure

I have a stored procedure where I insert a table into a temporary table and then I read that temp table row by row using a cursor:
USE [TEST_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[returnValidationFailures]
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 0 * INTO TempTbl FROM USER
DECLARE cursor1 CURSOR FAST_FORWARD FOR
SELECT *
FROM USER
OPEN cursor1
INSERT INTO TempTbl
EXEC ('fetch next from cursor1')
WHILE ##FETCH_STATUS = 0
INSERT INTO TempTbl
EXEC ('fetch next from cursor1')
CLOSE cursor1
DEALLOCATE cursor1
SELECT *
FROM TempTbl
DROP TABLE TempTbl
END
What I want here is to send the table name through a paramater like:
#TableNameParam varchar(10)
And then insert into temp table like:
SELECT TOP 0 * INTO TempTbl FROM #TableNameParam
DECLARE cursor1 CURSOR FAST_FORWARD FOR
SELECT *
FROM #TableNameParam
This doesn't work (obviously). But every other method I tried didn't work.
Is there any way I can set the data of an EXEC call like:
EXEC ('SELECT * FROM ' + #TableNameParam
' WHERE STATUS=1')
into the temp table?
Note: I DO NOT know the table structure.
I'm not very sure I understand what is your final scope, but my advise is to use a set-based approach.
To respond directly to you question, you should take into account the usage of temporary tables (#TempTable) instead of table variables (#TempTable).
EXEC ('SELECT * FROM ' + #TableNameParam + ' WHERE STATUS=1')
How is your data obtained in your temporary table?

How to insert result of a stored procedure and a varchar into a temp table?

I would like to insert into a temp table the result from a stored procedure in one column and a char in another column. I have something like this:
set #myvar = testvar
insert into #temp ( ID)
exec mysp
How can I add #myvar to #temp where #temp has two columns : ID and Var ?
Thanks
If the sp is maintained by you, you could pass the variable to the sp and get it returned with the other values.
If it is not mainained by you then you could define two tables , one to store the results from the sp and one to join them with your variable.
DECLARE #temp1 AS Table
(
// Columns returned by the sp
)
DECLARE #temp2 AS Table
(
MyVar VARCHAR(100),
// Columns returned by the sp
)
DECLARE #myvar VARCHAR(100)
SET #myvar = 'testvar'
insert into #temp1
exec mysp
INSERT INTO #Temp2
SELECT #myvar,*
FROM #Temp1