CREATE PROCEDURE cp_ttt
(
@tableSponsor nvarchar(20)
)
AS
declare @sqlTableCreate nvarchar(200)
set @sqlTableCreate = N' Create @tableName
( orgID nchar (10),
orgRegSubject nchar (10),
subjectTitle nvarchar (10),
subjectSummary nvarchar (10)
) '
execute sp_executesql @sqlTableCreate,
[email protected] nvarchar(20)',
@tableSponsor
GO
查询分析器调试,总是出现问题
服务器: 消息 170,级别 15,状态 1,行 1
[Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: [email protected]' 附近有语法错误。
------解决方案--------------------
- SQL code
alter PROCEDURE cp_ttt ( @tableSponsor nvarchar(20) ) AS declare @sqlTableCreate nvarchar(1000) set @sqlTableCreate = N'Create table [email protected]+' (orgID nchar (10), orgRegSubject nchar (10), subjectTitle nvarchar (10), subjectSummary nvarchar (10))' execute @sqlTableCreate
------解决方案--------------------
@tableSponsor 是干啥了,没用?
@tableName 没定义.
- SQL code
CREATE PROCEDURE cp_ttt @tableSponsor nvarchar(20) AS begin declare @sqlTableCreate nvarchar(200) declare @tablename as varchar(10) set @tablename = '...' -- or set @tablename = @tableSponsor set @sqlTableCreate = N'Create table ' + @tableName + '(orgID nchar (10), orgRegSubject nchar (10), subjectTitle nvarchar (10), subjectSummary nvarchar (10) )' exec(@sqlTableCreate)end go
------解决方案--------------------
- SQL code
CREATE PROCEDURE cp_ttt
(
@tableSponsor nvarchar(20)
)
AS
declare @sqlTableCreate nvarchar(1000)
set @sqlTableCreate = N'Create table '[email protected]+N'
(orgID nchar (10),
orgRegSubject nchar (10),
subjectTitle nvarchar (10),
subjectSummary nvarchar (10))'
execute sp_executesql @sqlTableCreate
GO
----
EXEC cp_ttt N'QQQ'
DROP TABLE QQQ
------解决方案--------------------
- SQL code
CREATE PROCEDURE cp_ttt(@tableSponsor nvarchar(20)) ASdeclare @sqlTableCreate nvarchar(1000) set @sqlTableCreate = N'Create table [email protected]+N' (orgID nchar (10), orgRegSubject nchar (10), subjectTitle nvarchar (10), subjectSummary nvarchar (10))' execute sp_executesql @sqlTableCreateGO ----测试存储过程EXEC cp_ttt N'QQQ'DROP TABLE QQQ
------解决方案--------------------