且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MS SQL Server中将内联SQL转换为存储过程

更新时间:2023-01-29 12:56:55

它应该如下所示:

 创建  PROCEDURE  TheNameOfProcedure 
@ reportgroupcode < TypeOfData>,
@ AnotherCondition < TypeOfData>
AS
BEGIN
SELECT DisplayName,reportcode
FROM mstreports
WHERE reportgroupcode = @ reportgroupcode AND active = 1 AND @ AnotherCondition
ORDER BY reportcode
END



其中:

< TypeOfData> 表示其中之一:数据类型(T-SQL) [ ^ ]

@rertrtgroupcode 来自 myData.Tables [0] .Rows [i] [1]

@AnotherCondition 来自 sql_text.Tables [0] .Rows [0] [0] .ToString()


您应该对查询进行参数化,以使其成为正确的SQL存储过程。



SQL逻辑将是相同的,但是你会将代码中的@variables(参数)传递给SQL。



可能你应该在这里查看这个指南,它会有所帮助你进一步为sql商店程序创建复杂的动态内联查询。



优化存储过程 [ ^ ]



希望这会对你有帮助。

How to convert below SQL inline Query to Stored Procedure in MS SQL Server ?

Dataset sql_text = ISQL.ExecuteDataSet("SP_ParameterValAdmin");

sql_command = sql_text.Tables[0].Rows[0][0].ToString();

sql_command =
select DisplayName,reportcode
from mstreports
where reportgroupcode= + Convert(myData.Tables[0].Rows[i][1]) +  and active=1 + sql_command +
order by reportcode

It should looks like:
CREATE PROCEDURE TheNameOfProcedure
    @reportgroupcode <TypeOfData>,
    @AnotherCondition <TypeOfData>
AS
BEGIN
    SELECT DisplayName, reportcode
    FROM mstreports
    WHERE reportgroupcode= @reportgroupcode  AND active=1 AND @AnotherCondition
    ORDER BY reportcode
END


where:
<TypeOfData> means one of that: Data Types (T-SQL)[^]
@reposrtgroupcode comes from myData.Tables[0].Rows[i][1]
@AnotherCondition comes from sql_text.Tables[0].Rows[0][0].ToString()


You should make your query parameterized in order to make it proper SQL Store Procedure.

The SQL logic will be the same however you will be passing @variables(parameters) form your code to SQL.

Probably you should check this guidelines here, It will help you overall further for creating complex dynamic inline queries to sql store procedures.

Optimize-stored-procedures[^]

Hope this will help you.