Is there a way to have SQL server validate object references in Stored Procs?

后端 未结 7 2089
半阙折子戏
半阙折子戏 2021-01-13 20:12

The following code executes fine in SQL Server

create proc IamBrokenAndDontKnowIt as
select * from tablewhichdoesnotexist

Of course if I tr

7条回答
  •  时光说笑
    2021-01-13 21:13

    You could check information_schema.tables to check whether a table exist or not and then execute the code

    here is quickly slung function to check

     create function fnTableExist(@TableName varchar(64)) returns int as
        begin
            return (select count(*) from information_schema.tables where table_name=@tableName and Table_type='Base_Table')
        end
    
    go        
    
        if dbo.fnTableExist('eObjects') = 0 
            print 'Table exist'
        else
            print 'no suchTable'
    

    like wise you can check for existance of Stored procs / functions in

    .INFORMATION_SCHEMA.ROUTINES.Routine_name for th name of the Stored proc/function

提交回复
热议问题