用于查询 Adventureworks 表的动态 SQL
我试图从 Sql Server 2008 中的“Person”模式查看 Adventureworks DB 中的表列表。我开发了以下 SP,但按如下方式运行后,出现错误“')' 附近的语法不正确”。你知道我如何修改这个 SP 或 exec 语句吗?
CREATE PROCEDURE [getTableNames]
@SchemaName VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
SET @SchemaName = 'PERSON'
DECLARE @cmd AS VARCHAR(max)
SET @SchemaName = RTRIM(@SchemaName)
SET @cmd = N'SELECT TABLE_NAME ' +
'FROM information_schema.Tables ' +
'WHERE TABLE_TYPE = ''BASE TABLE'' AND TABLE_SCHEMA = @SchemaName'
END
exec sp_executesql getTableNames, N'@SchemaName NVARCHAR(50), @SchemaName'
I am trying to see a list of tables from Adventureworks DB from "Person" schema in Sql Server 2008. I developed the following SP, but after running it as follows it gives me error "Incorrect syntax near ')'". Do you know how I can revise this SP or exec statement?
CREATE PROCEDURE [getTableNames]
@SchemaName VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
SET @SchemaName = 'PERSON'
DECLARE @cmd AS VARCHAR(max)
SET @SchemaName = RTRIM(@SchemaName)
SET @cmd = N'SELECT TABLE_NAME ' +
'FROM information_schema.Tables ' +
'WHERE TABLE_TYPE = ''BASE TABLE'' AND TABLE_SCHEMA = @SchemaName'
END
exec sp_executesql getTableNames, N'@SchemaName NVARCHAR(50), @SchemaName'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上不需要在这里使用动态 SQL,而且您的存储过程不太正确,因为您没有执行 @cmd 语句。只需使用:
You don't actually need to use dynamic SQL here, plus your sproc isn't quite right as you're not executing the @cmd statement. Just use:
您不需要动态 SQL:
You don't need dynamic SQL: