1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| CREATE PROCEDURE p_table_exists_in_database_schema
@database nvarchar(128)
, @destination_schema nvarchar(128)
, @destination_table nvarchar(128)
AS
BEGIN
SET NOCOUNT ON
DECLARE @table_exists bit
, @sql nvarchar(max) = '
IF EXISTS
(
SELECT *
FROM ' + @database + '.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = @_destination_schema
AND TABLE_NAME = @_destination_table
)
SET @_ok = 1
ELSE SET @_ok = 0'
EXEC sp_executesql
@sql
, N'@_ok bit OUTPUT, @_destination_schema nvarchar(128), @_destination_table nvarchar(128)'
, @_destination_schema = @destination_schema
, @_destination_table = @destination_table
, @_ok = @table_exists OUTPUT
SELECT @database AS database_name
, @destination_schema AS [schema_name]
, @destination_table AS table_name
, @table_exists AS table_exists
/* -- Ou plus simplement :
SELECT @table_exists AS table_exists
*/
END |
Partager