I would like to know if it is possible and how to have a temporary table as output parameter in a stored procedure. I know that normal tables can not be modified with UPDATE and are only READONLY tables, so I thought of using temp tables. But I don't understand how to pass them to the stored procedure and how to have them as output:
CREATE TABLE #tableBER (ID INT NOT NULL IDENTITY PRIMARY KEY, VAL INT NOT NULL DEFAULT 0);
CREATE TABLE #tableRL (ID INT NOT NULL IDENTITY PRIMARY KEY, VAL INT NOT NULL DEFAULT 0);
ALTER PROCEDURE [dbo].[getFound]
#ber #tableBER OUTPUT,
#rl #tableRL OUTPUT,
AS
BEGIN
...
UPDATE #ber ... --UPDATE
UPDATE #rl ...
SELECT #ber; -- RETURN THE TWO TABLES
SELECT #rl;
END;
Is it possible to do such thing?Thanks!
A stored procedure may reference and use temporary tables that are created in an outer scope. There's no need to mark them as parameters, just create the tables, call the stored procedure, and then inspect the contents:
create procedure DoStuff
as
insert into #TTT (ID) values (1),(2),(3)
And usage:
create table #TTT(ID int not null)
exec DoStuff
select * from #TTT
Results:
ID
-----------
1
2
3
Related
I want to create a stored procedure to return all column data from a table except one column that key of where condition. The original table will be change a lot during my project, so I don't want to create table with declaring columns. How can I create the table that columns are copy of another table? I'm using SQL Server.
ALTER PROCEDURE [dbo].[SelectCharacterDataById]
#Id INT
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #TempTable
AS
SELECT *
FROM dbo.CharacterData
WHERE CharacterID = #Id
ALTER TABLE #TempTable
DROP COLUMN CharacterID
SELECT *
FROM #TempTable
END
I am running a stored procedure that selects all the records from a database.
Select *
from dbo.Recods
where Active = 1;
but when I run this stored procedure, I do not get the identity key back, just the rest of the columns in the database.
Any ideas?
Thank you
The query to create the table is:
CREATE TABLE [dbo].[Tournaments] (
-- Add the parameters for the stored procedure here
Id int IDENTITY(1,1) PRIMARY KEY,
TournamentName nvarchar(50) NOT NULL,
EntryFee float,
Active int
);
I'm trying to write an SQL function that given a name of a game, it will allow me to search the table of games for that particular game and returns all the info about that game.
This is the code for the Games table:
CREATE TABLE Games(
game_id INT IDENTITY PRIMARY KEY,
name VARCHAR(50),
release_date VARCHAR(50),
rating VARCHAR(5),
min_age INT,
development_team_email VARCHAR(50) FOREIGN KEY REFERENCES Development_Teams,
release_conference INT FOREIGN KEY REFERENCES Conferences
)
And here is the what I could come up with when I was trying to write the function:
create function SearchGames(#game_name varchar(50))
returns table
begin
declare #game
Select (*)
From Games
where Games.name = #game_name
return #game
end
I'm getting a lot of syntax errors and I don't know what I'm doing wrong. Any help appreciated.
Use inline table valued function syntax and add schema:
create function dbo.SearchGames(#game_name varchar(50))
returns table
AS
RETURN (Select *
From Games
where Games.name = #game_name);
SqlFiddleDemo
If you use stored procedure you need to use:
CREATE TABLE ...;
INSERT INTO ... EXEC stored_procedure #args;
-- another operation on stored procedure resultset
while with inline table function you just:
SELECT * FROM dbo.SearchGames('aaa') GROUP BY ... HAVING ... ORDER BY;
I wouldn't recommend using a function for this, but rather a stored procedure:
Create Proc spSearchGames (#game_name Varchar (50))
As Begin
Select *
From Games
Where name = #game_name
End
Go
And executing it:
Exec spSearchGames 'YourGameName'
Lets say I have a CREATE TABLE code like this:
CREATE TABLE Test (
ID int NOT NULL IDENTITY(1,1),
SortIndex int,
Name nvarchar(50) NOT NULL
);
I was wondering if it's possible to make a table in MSSQL which had the ability to insert the ID's value into the SortIndex column when I run an INSERT.
So I would run this INSERT:
INSERT INTO Test (Name) VALUES ('Awesome Dude');
Which would normally yield the row:
ID,SortIndex,Name
1,NULL,"Awesome Dude"
But I'd like it to automatically be:
ID,SortIndex,Name
1,1,"Awesome Dude"
Is this even possible by altering the CREATE TABLE script, or do I have to use a TRIGGER?
I would be inclided to take a slightly different approach to this. If you want your SortIndex to default to the ID, but be overridable, I would use a nullable column, and a computed column:
CREATE TABLE Test (
ID int NOT NULL IDENTITY(1,1),
OverrideSortIndex int,
Name nvarchar(50) NOT NULL,
SortIndex AS ISNULL(OverrideSortIndex, ID)
);
If you need to change the sort index for any reason, update the column OverrideSortIndex and this takes precedence.
I've created a insert stored procedure with two tables like in the exapmle:
Table NameAge
CREATE TABLE [dbo].[Assignment3_NameAge]
(
userID int PRIMARY KEY IDENTITY(1,1),
Name varchar(255) NOT NULL,
Age int NOT NULL
)
Table Hobbies
CREATE TABLE [dbo].[Assignment3_Hobbies]
(
hobbiesID int Identity(1,1) Primary Key,
userID int Foreign Key references Assignment3_NameAge(userID),
hobbies varchar(255) NOT NULL,
)
Insert Stored Procedure
CREATE PROCEDURE [dbo].p_Assignment3Join_ins
#Name nvarchar(100),
#Age int,
#Hobbies nvarchar(100)
AS
INSERT INTO [TABLE].[dbo].[Assignment3_NameAge]
([Name]
,[Age])
VALUES (#Name,#Age)
INSERT INTO [TABLE].[dbo].[Assignment3_Hobbies]
([Hobbies])
VALUES (#Hobbies)
The problem is that when i run the stored procedure the table Hobbies has a null value for userid(the foreign key)
What am i doing wrong?
You should provide the key of the Assignment3_NameAge value you want to insert into Assignment3_Hobbies.
If you want the last inserted you can use SCOPE_IDENTITY() from SQL Server(if you're using SQL Server) or equivalent. It will give you the last inserted value from Assignment3_NameAge
I am guessing this is SQL Server based on the IDENTITY column. Correct?
The first insert creates a user, but there is no user ID being set on the insert of the hobby. You need to capture the identity value from the first insert to be used in the second insert. Have you gon over the system functions available?
You're not supplying a value for it, SQL won't automagically fill the value in for you even though you've created a Foreign Key relationship. It's your job to populate the tables.