问题描述
我面临一个问题。我必须在syntax.so中使用proc输入参数值创建一个表。如何在不给出任何硬编码值的情况下这样做。
我尝试过:
CREATE PROC UPDATE_AUDIT_HISTORIC
(@ table_name varchar(50))
as
begin
- 检查GIVEN TABLE NAME的审计标志值
if(从config_table中选择audit_flag,其中table_name = @ table_name)='Y'
BEGIN
- 检查是否存在于表中的记录栏
IF(SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @ table_name)NOT IN('crt_dt','crt_by','mod_dt','mod_by')
ALTER TABLE @table_name ADD crt_dt DATE,crt_by VARCHAR(50),mod_dt DATE,mod_by DATE
END
--CHECKING hist_flag VALUE FOR GIVEN TABLE NAME
if(从config_table中选择hist_flag,其中table_name = @ table_name)=是'
BEGIN
- 检查历史记录表是否存在于数据库中并创建它
IF(SELECT COUNT(*)FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @ table_name)= 0
BEGIN
CREATE TABLE Tbl_History
(
table_name_hist_ID int主键,
Table_name varchar(50),
column_s varchar(max)
)
END
结束
结束
这个我的proc如果运行这个proc我将是得到错误,如消息102,级别15,状态1,过程UPDATE_AUDIT_HISTORIC,第12行
'@ table_name'附近的语法不正确。
所以我们如何在create table语法中传递值..
这是错误行ALTER TABLE @table_name ADD crt_dt DATE,crt_by VARCHAR(50),mod_dt DATE, mod_by DATE。
这里我不能使用varible name如何创建一个表名相同的变量值...请帮助我。非常感谢...
I am facing one issue.i have to create one table using proc input parameter value in syntax.so how can we do that without giving any hardcode value.
What I have tried:
CREATE PROC UPDATE_AUDIT_HISTORIC
(@table_name varchar(50))
as
begin
--CHECKING AUDIT FLAG VALUE FOR GIVEN TABLE NAME
if (select audit_flag from config_table where table_name=@table_name)='Y'
BEGIN
--CHECKING AUDIT COLUMNS EXIST OR NOT IN THE TABLE
IF(SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=@table_name) NOT IN ('crt_dt', 'crt_by', 'mod_dt', 'mod_by')
ALTER TABLE @table_name ADD crt_dt DATE,crt_by VARCHAR(50),mod_dt DATE,mod_by DATE
END
--CHECKING hist_flag VALUE FOR GIVEN TABLE NAME
if (select hist_flag from config_table where table_name=@table_name)='Y'
BEGIN
--CHECKING HISTORY TABLE IS EXIST OR NOT IN THE DATABASE AND CREATE IT
IF(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=@table_name)=0
BEGIN
CREATE TABLE Tbl_History
(
table_name_hist_ID int primary key,
Table_name varchar(50),
column_s varchar(max)
)
END
END
END
this my proc if run this proc i would be getting error like Msg 102, Level 15, State 1, Procedure UPDATE_AUDIT_HISTORIC, Line 12
Incorrect syntax near '@table_name'.
So how we can pass the value in create table syntax..
this is the error line ALTER TABLE @table_name ADD crt_dt DATE,crt_by VARCHAR(50),mod_dt DATE,mod_by DATE.
Here i can't use varible name how can create a table same name of variale value..please assist me.any answer would be greatly appreciated...
推荐答案
DECLARE @table_name nvarchar(50);
DECLARE @CreateTable_SQL nvarchar(MAX);
SET @table_name='Sorry';
SET @CreateTable_SQL='CREATE TABLE '+@table_name+'(id int identity not null,toWhom nvarchar(50),why nvarchar(MAX),[Status] nvarchar(50) )';
EXEC(@CreateTable_SQL)
-- table created check it
INSERT INTO Sorry VALUES('S','that reason','Pending')
SELECT * FROM Sorry
这篇关于我们可以使用变量名创建表而不是给出硬编码名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!