目前我必须创建一个包含所有表格的列表。在几个(非常)长的SQL语句中使用的方案。 当然,我可以手动完成SQL语句并记下JOIN,Subselect等中使用的每个表。
但如果有更简单的方法,我会很好奇。也许有一个“简单”的SQL语句,但我不知道如何做到这一点。
我为什么要问?我必须为大约50个SQL-Unload-Statements执行此操作,其中所有SQL-Unload-Statements都使用30到70个表。我想你可以想象这个很无聊的作品
提前感谢任何输入&提示!
答案 0 :(得分:1)
请在此处阅读:How can i get the list of tables in the stored procedure
;WITH stored_procedures AS (
SELECT
o.name AS proc_name, oo.name AS table_name,
ROW_NUMBER() OVER(partition by o.name,oo.name ORDER BY o.name,oo.name) AS row
FROM sysdepends d
INNER JOIN sysobjects o ON o.id=d.id
INNER JOIN sysobjects oo ON oo.id=d.depid
WHERE o.xtype = 'P')
SELECT proc_name, table_name FROM stored_procedures
WHERE row = 1
ORDER BY proc_name,table_name
所有信用都归功于上面线程中上面代码的OP。
答案 1 :(得分:1)
我会发布这个答案&将其标记为已解决,因为这是解决我的问题的最佳和最简单的方法。 提示是@whereisSQL的评论(非常感谢!)
似乎已经在这个帖子中被问过了,但我没有找到它:
https://dba.stackexchange.com/questions/121346/need-to-get-identify-all-tables-and-columns-in-a-sql-query/121355#121355
有一个在线" get table列"这里列出SQL语句表的工具: http://107.170.101.241:8080/getTableColumn/
它列出了语句中使用的所有表和其他列!在我的案例中完美的解决方案。
答案 2 :(得分:0)
这可能会有所帮助。使用DMV
IF EXISTS (
SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[sp_dependsobj]')
AND type IN (
N'P'
,N'PC'
)
)
DROP PROCEDURE [dbo].[sp_dependsobj]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_dependsobj] (@Objname NVARCHAR(2000))
AS
BEGIN
SET NOCOUNT ON
DECLARE @ObjID INTEGER
DECLARE @ObjType VARCHAR(100)
DECLARE @RowCount INTEGER = 0
SELECT @ObjID = OBJECT_ID(@Objname)
SELECT @ObjType = TYPE
FROM sys.objects
WHERE object_id = @ObjID
DECLARE @RefObjects AS TABLE (
[Object_name] SYSNAME
,[Type] VARCHAR(255)
,referenced_entity_name SYSNAME
,Column_name SYSNAME NULL
,referenced_entity_type VARCHAR(255)
)
DECLARE @FoundIn AS TABLE (
[Object_name] SYSNAME
,[Found In] SYSNAME
,[Type] VARCHAR(255)
)
DECLARE @SQLStr VARCHAR(MAX)
SET @SQLStr = '
WITH CTE_Objects
AS
(
SELECT o.NAME referencing_entity_name
,o.type_desc referencing_object_type
,sed.referenced_entity_name
,refobj.referenced_minor_name
,obj.type_desc
,obj.object_id
FROM sys.sql_expression_dependencies sed
INNER JOIN sys.objects o ON sed.referencing_id = o.[object_id]
INNER JOIN sys.objects obj ON sed.referenced_entity_name = obj.name
INNER JOIN sys.dm_sql_referenced_entities(''dbo.' + @Objname + ''', ''OBJECT'') refobj ON
refobj.referenced_id = sed.referenced_id
WHERE o.NAME = ''' + @Objname + '''
UNION
SELECT ''' + @Objname + ''', (SELECT type_desc from sys.objects where name = ''' + @Objname + '''), referenced_entity_name, NULL, obj.type_desc
,obj.object_id
FROM sys.dm_sql_referenced_entities (''dbo.' + '' + @Objname +
''', ''OBJECT'') refObj INNER JOIN sys.objects obj
ON refObj.referenced_entity_name = obj.name
)
SELECT CTE.referencing_entity_name Object_name, CTE.referencing_object_type Type, CTE.referenced_entity_name, CTE.referenced_minor_name Column_name, CTE.type_desc referenced_entity_type
FROM CTE_Objects CTE LEFT JOIN sys.columns cl ON
cte.object_id = cl.object_id AND cte.referenced_minor_name = cl.name
Order by cte.referencing_entity_name, CASE WHEN cte.referencing_entity_name IS NULL THEN 0 ELSE cl.column_id END
'
INSERT INTO @RefObjects (
[Object_name]
,[Type]
,referenced_entity_name
,Column_name
,referenced_entity_type
)
EXEC (@SQLStr)
SET @RowCount = @@ROWCOUNT
IF @RowCount > 0
BEGIN
SELECT *
FROM @RefObjects
END
SET @SQLStr = '
SELECT DISTINCT [Object Name] = o.NAME
,[Found In] = sp.NAME
,sp.type_desc Type
FROM sys.objects o
INNER JOIN sys.sql_expression_dependencies sd ON o.object_id = sd.referenced_id
INNER JOIN sys.objects sp ON sd.referencing_id = sp.object_id
WHERE o.NAME = ''' + @Objname + '''
UNION
SELECT ''' + @Objname + ''', referencing_entity_name, (SELECT type_desc from sys.objects where name = referencing_entity_name)
FROM sys.dm_sql_referencing_entities (''dbo.' + '' + @Objname + ''', ''OBJECT'');
'
INSERT INTO @FoundIn (
[Object_name]
,[Found In]
,[Type]
)
EXEC (@SQLStr)
SET @RowCount = @@ROWCOUNT
IF @RowCount > 0
BEGIN
SELECT *
FROM @FoundIn
END
IF NOT EXISTS (
SELECT 1
FROM @RefObjects
)
AND NOT EXISTS (
SELECT 1
FROM @FoundIn
)
BEGIN
PRINT 'Object does not reference any object, and no objects reference it.'
END
SET NOCOUNT OFF
END
GO