ad-hoc sql脚本中的程序化T-SQL

时间:2022-04-21 09:08:21

Is it possible to use T-SQL programming constructs outside the confines of stored procedures and functions? Specifically, can they be used in ad-hoc sql scripts? If so, is the full range of capabilities available (aside from passing in parameters and returning values that I guess would only be supported in stored procedures and functions)?

是否可以在存储过程和函数范围之外使用T-SQL编程结构?具体来说,它们可以用于ad-hoc sql脚本吗?如果是这样,是否有可用的全部功能(除了传递参数和返回值,我想这只会在存储过程和函数中得到支持)?

I'm from an Oracle PL/SQL background. PL/SQL can be used in stored procedures and functions, but also within anonymous PL/SQL code blocks that can be used in ad-hoc sql scripts and do not get stored in the DB. I'd like to know if T-SQL has similar capabilities.

我来自Oracle PL / SQL背景。 PL / SQL可用于存储过程和函数,也可用于可在ad-hoc sql脚本中使用且不存储在DB中的匿名PL / SQL代码块。我想知道T-SQL是否具有类似的功能。

1 个解决方案

#1


Yes they can take this example solution to the fizzbuzz problem:

是的,他们可以采用这个示例解决方案来解决fizzbuzz问题:

declare @counter int
declare @output varchar(15)

set @counter = 1
while @counter < 101
begin
    set @output = ''
    if @counter % 3 = 0
        set @output = 'Fizz'
    if @counter % 5 = 0
        set @output = @output + 'Buzz'
    if @output =''
        set @output = @counter
    print @output
    set @counter = @counter + 1
end

Of course don't fall into the trap of using procedural code to work with data, SQL works best treating data as a set.

当然不要陷入使用过程代码来处理数据的陷阱,SQL最好将数据作为一个集处理。

#1


Yes they can take this example solution to the fizzbuzz problem:

是的,他们可以采用这个示例解决方案来解决fizzbuzz问题:

declare @counter int
declare @output varchar(15)

set @counter = 1
while @counter < 101
begin
    set @output = ''
    if @counter % 3 = 0
        set @output = 'Fizz'
    if @counter % 5 = 0
        set @output = @output + 'Buzz'
    if @output =''
        set @output = @counter
    print @output
    set @counter = @counter + 1
end

Of course don't fall into the trap of using procedural code to work with data, SQL works best treating data as a set.

当然不要陷入使用过程代码来处理数据的陷阱,SQL最好将数据作为一个集处理。