面临“默认架构不存在"的错误.使用 exec() 在 sp 内部执行运行时查询时 [英] Facing error of "The default schema does not exist." when executing runtime query inside sp using exec()

查看:14
本文介绍了面临“默认架构不存在"的错误.使用 exec() 在 sp 内部执行运行时查询时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 sp 中进行了运行时查询,并且正在使用 exec() 在 sp 中执行查询,但是在创建 sp 时出现错误

i have made a runtime query inside a sp and am exceuting the query within the sp using exec(), but when creating the sp i am getting the error

The default schema does not exist.

SP 是:

CREATE PROCEDURE MySP
    @tableName varchar(100)

AS
BEGIN   
    SET NOCOUNT ON;

declare @selectQuery varchar(MAX)

set @selectQuery = 'select * from ' + @tableName

exec(@selectQuery)

end

请帮忙

推荐答案

使用 CREATE PROCEDURE dbo.MySP

您登录的用户必须具有不存在的默认架构.

The user you are logged in as must have a non existent default schema.

DEFAULT_SCHEMA 可以设置为数据库中当前不存在的架构.

此外,您应该使用 quotename(@tableName)sysname 的参数类型而不是 varchar(100) 以避免 SQL 注入或只是来自非标准对象名称的错误.

Also you should use quotename(@tableName) and a parameter type of sysname rather than varchar(100) to avoid SQL injection or just errors from non standard object names.

这篇关于面临“默认架构不存在"的错误.使用 exec() 在 sp 内部执行运行时查询时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆