gpt4 book ai didi

sql - 列数 每个过程返回 T-Sql

转载 作者:行者123 更新时间:2023-12-02 04:50:40 24 4
gpt4 key购买 nike

我的任务是找出 sys.procedures 中每个过程返回(选择)的列数?程序看起来像

    Create PROCEDURE [dbo].[stpSSC_DMND001]
@Event nvarchar(3),
@FunctionalArea nvarchar(10),
@Venue nvarchar(10)
As

BEGIN
BEGIN TRY

If @Event is null or @Event=''
RAISERROR('@Event can not be null or empty string',16,1);
Else if not exists(select * from ENVIRONMENT where CODE=@Event)
RAISERROR('Value "%s" for @Event not found in ENVIRONMENT',16,1, @Event);

IF @FunctionalArea='All'
Set @FunctionalArea=Null

IF @Venue='All'
Set @Venue=Null
SELECT DISTINCT ENV.NAME AS [Event],
ORG.CODE AS [FA code],
ORG.NAMEAS [FA description],
LOC.CODE AS [Venue code]
FROM
POSITION AS POS
LEFT OUTER JOIN
ENVIRONMENT AS ENV ON ENV.CODE = POS.EVENT
WHERE ENV.CCODE_ENV=@Event
AND (@FunctionalArea IS NULL OR org.IDENTIFIER=@FunctionalArea)
AND (@Venue IS NULL OR loc.IDENTIFIER=@Venue)
ORDER BY [FA code],
[Venue code]
END TRY

/* ERROR HANDLING */
BEGIN CATCH
Declare @ErrMsg nvarchar(200) = ERROR_MESSAGE(),
RETURN @ErrNum;
END CATCH
END

我在谷歌上找不到任何熟悉的东西。有什么建议么?

最佳答案

这大部分时间都有效,使用存储过程的静态分析,但有一些存储过程不适用于:

SELECT
procedures.name AS ProcedureName
,COUNT(*) AS FirstResultSetColumnCount
FROM
sys.procedures
CROSS APPLY
(
SELECT 1 as counter
FROM sys.dm_exec_describe_first_result_set_for_object( procedures.object_id,1)
WHERE error_state IS NULL
) metadata
GROUP BY procedures.name

编辑:查看以下 MSDN 文章,了解函数 sys.dm_exec_describe_first_result_set_for_object 返回错误代码的可能原因:

https://msdn.microsoft.com/en-us/library/ff878236.aspx

编辑:这将对您有所帮助,但这并不是您想要的。如果您想使用 C#,我相信您可以使用 SqlDataReader 类(命名空间:System.Data.SqlClient/程序集:System.Data)作为自动化解决方案。

DECLARE @ParameterInfo TABLE
(
ProcedureId INT
,ProcedureName SYSNAME
,ParameterName SYSNAME
,TypeName SYSNAME
,UNIQUE(ProcedureName,ParameterName)
)

INSERT INTO @ParameterInfo
SELECT
DENSE_RANK() OVER (ORDER BY procedures.object_id) AS procedureId
,procedures.name
,parameters.name
,types.name
FROM sys.procedures
JOIN sys.parameters
ON procedures.object_id = parameters.object_id
JOIN sys.types
ON parameters.user_type_id = types.user_type_id
where
has_default_value = 0
AND CAST(
case
when procedures.is_ms_shipped = 1 then 1
when (
select
major_id
from
sys.extended_properties
where
major_id = procedures.object_id and
minor_id = 0 and
class = 1 and
name = N'microsoft_database_tools_support')
is not null then 1
else 0
end AS BIT) = 0

DECLARE @Counter INT = 1;
DECLARE @ProcedureCount INT = (SELECT MAX(ProcedureId) FROM @ParameterInfo);
DECLARE @ProcedureName SYSNAME;
DECLARE @ParameterText VARCHAR(MAX) = '';

WHILE @Counter <= @ProcedureCount
BEGIN

SELECT
@ProcedureName = ProcedureName
,@ParameterText =
@ParameterText + ', '
+ ParameterName + ' = '
+ CASE
TypeName
WHEN 'INT' THEN '0'
WHEN 'DATE' THEN '''1/1/1900'''
WHEN 'DATETIME' THEN '''1/1/1900'''
--More type defaults here...
ELSE '0'
END

FROM
@ParameterInfo
WHERE
ProcedureID = @Counter

DECLARE @SQL VARCHAR(MAX) = 'SET FMTONLY ON; EXEC ' + @ProcedureName + SUBSTRING(@ParameterText,2,8000) + '; SET FMTONLY OFF;'

PRINT @SQL;
PRINT '---';

SET @Counter = @Counter + 1;
END

编辑:或者您可以在本地机器上安装 SQLServer 2012 Express 或开发人员版。

关于sql - 列数 每个过程返回 T-Sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28979639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com