我将通过SQL查询创建过程并拥有@name_table
(nvarchar
)。
如何使用多个表的一个程序不使用EXEC
?
示例:
CREATE TABLE tbl_produces
(
id int primary key,
name nvarchar(250)
)
GO
INSERT INTO tbl_produces VALUES (1, 'abc'), (2, 'cde'), (3, 'hfd')
GO
CREATE PROCEDURE return_table
(
@table_name nvarchar(250)
)
AS
SELECT * FROM @table_name
RETURN
GO
EXEC return_table 'tbl_produces' " = " SELECT * FROM tbl_produces
"
我该怎么做?帮我!谢谢你的帮助
我不想使用
CREATE PROCEDURE return_table
(
@table_name_ nvarchar(250)
)
AS
DECLARE @sqlStr nvarchar(max) ,
@ParmDefinition nvarchar(MAX)
Set @sqlStr = 'SELECT * FROM @table_name'
SET @ParmDefinition = N'@table_name nvarchar(250),
EXECUTE sp_executesql @sqlStr,
@ParmDefinition,
@table_name = @table_name_
RETURN
GO
我不想使用EXECUTE可以返回表
可以查看您是否使用查询
CREATE PROCEDURE return_table
(
@table_name_ nvarchar(250)
)
AS
DECLARE @sqlStr nvarchar(max) ,
@ParmDefinition nvarchar(MAX)
Set @sqlStr = 'SELECT * FROM @table_name'
SET @ParmDefinition = N'@table_name nvarchar(250),
EXECUTE sp_executesql @sqlStr,
@ParmDefinition,
@table_name = @table_name_
RETURN
GO
当我想要创建1个函数时用于多个表
CREATE FUNCTION [dbo].[DanhSachNhaCungCap] ( ) -- Thong Tin Nha Cung Cap
RETURNS TABLE
AS
RETURN
(
select * from
(
SELECT 'EXEC return_table ''tblProduct_01''' AS sp_executesql
UNION ALL
SELECT 'EXEC return_table ''tblProduct_02''' AS sp_executesql
) AS T
)
GO
- >它失败了
答案 0 :(得分:2)
试试这个:
CREATE TABLE tbl_produces
(
id int primary key,
name nvarchar(250)
)
GO
INSERT INTO tbl_produces VALUES (1, 'abc'), (2, 'cde'), (3, 'hfd')
GO
CREATE PROCEDURE return_table
(
@table_name nvarchar(250)
)
AS
DECLARE @V_Query NVARCHAR(MAX);
SET @V_Query = 'SELECT * FROM ' + @table_name;
EXEC sp_executesql @V_Query
GO
EXEC return_table 'tbl_produces'
答案 1 :(得分:1)