Creating a SQL table variable in a stored procedure
Asked Answered
C

2

6

I want to create SQL variable table in stored procedure which include this;

Select a,b,c,d from **@tablename** where a=1 and c=0

How can do this with sp when creating sp?

Cloyd answered 28/7, 2015 at 10:46 Comment(4)
Refer this link codeproject.com/Articles/18972/…Panamerican
You need to use dynamic SQL to include the table name as a parameter in a SQL query.Negligible
This is a sign of bad database design.Lcm
Which DBMS are you using? "SQL" is a query language, not a specific DBMS product.Precipitancy
A
9

You can declare table variable in SP as:

DECLARE @tablename TABLE(
    a INT,
    b INT,
    c INT,
    d INT);

SELECT * FROM @tablename;
Azoth answered 28/7, 2015 at 11:24 Comment(0)
P
0

I hope this answer helps you

GO
CREATE PROCEDURE PrededureName
AS
BEGIN
DECLARE @tempTable TABLE
(
    a INT, 
    b INT,
    c INT,
    d INT
)
INSERT INTO @tempTable (a, b, c, d)
SELECT a, b, c, d FROM @tablename WHERE a=1 and c=0
END
GO;
Prunella answered 18/5, 2019 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.