本文介绍了验证动态指定的表存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要编写一个存储过程来验证表是否存在,并且 还表示执行存储过程的用户可以访问 指定的表。 /> 任何用户都可以调用此公开程序并传递数据库 名称,所有者名称和表名作为参数。如果表存在并且用户可以访问它,则程序 返回成功,或者如果他没有,则b $ b失败。这是我所拥有的简化版本,但 我想知道是否有更好的方法。谢谢。 创建程序愚蠢如下 开始 声明@myError int, @mytable varchar(128), @myquery varchar(128) 选择@mytable =''[Northwind]。[dbo]。[sysobjects2]'' 选择@myquery =''DECLARE @x int SELECT @x = count(1)from''+ @mytable +''其中1 = 2'' exec(@myquery) 选择@myError = @@ ERROR if @myError!= 0 BEGIN RAISERROR(''错误:指定的表%s无法访问。'',10,1, @mytable) RETURN 1 结束 结束 go I need to write a stored procedure to verify that a table exists andalso that the user executing the stored procedure has access to thespecified table. Any user can call this publicly available procedure and pass a databasename, an owner name and a table name as parameters. The procedurereturns success if the table exists and the user has access to it, orfails if he doesn''t. Here''s a simplified version of what I have, butI''m wondering if there''s a better way. Thanks. create procedure dumb asbegindeclare @myError int,@mytable varchar(128),@myquery varchar(128) select @mytable = ''[Northwind].[dbo].[sysobjects2]''select @myquery = ''DECLARE @x int SELECT @x = count(1) from '' +@mytable + '' where 1 = 2''exec (@myquery)select @myError = @@ERRORif @myError != 0BEGINRAISERROR (''ERROR: The specified table %s cannot be accessed.'', 10, 1,@mytable)RETURN 1end endgo 推荐答案 是的,似乎你会有一些动态SQL在源数据库上执行a USE,然后将权限结果() 分配给输出参数。你可以使用sp_executesql。 - Erland Sommarskog,SQL Server MVP, es **** @ sommarskog.se SQL Server SP3的联机书籍 http://www.microsoft.com/sql/techinf...2000/books.asp 这篇关于验证动态指定的表存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 23:49