如何在SQL Server存储过程中的用户定义表类型上进行ForEach? [英] How to do ForEach on user defined table type in SQL Server stored procedure?

本文介绍了如何在SQL Server存储过程中的用户定义表类型上进行ForEach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XX PROCEDURE [dbo].[XXX]
    @X dbo.IntType readonly
AS
BEGIN
    SET NOCOUNT ON;
    // how can I foreach(@X) here and do process individually?
END

IntType是用户定义的表类型

CREATE TYPE [dbo].[IntType] AS TABLE(
    [T] [int] NOT NULL,
    PRIMARY KEY CLUSTERED 
(
    [T] ASC
)

我需要在SQL Azure中使用它,请咨询.

I need to use this in SQL Azure, please advice.

推荐答案

光标与ForEach的SQL等效,

Cursors are the SQL equivalent of ForEach,

但是游标通常是不良SQL的征兆:它们违反了通常的基于集的思想,即SQL是基于SQL并针对SQL进行优化的.

But cursors are often a sign of bad SQL: they violate the usual set-based thinking that SQL is built on and optimized for.

SQL cursorSQL Cursor Azure上搜索许多示例教程优化注释.

Search on SQL cursor or SQL Cursor Azure for many examples, tutorials and optimization notes.

但是还不能说足够多:避免使用游标:它们经常是SQL中其他语言的程序员的拐杖,而且它们通常运行缓慢且难以维护.

But it can't be said enough: avoid cursors: they are often the crutch for programmers from other languages in SQL and they are often slow and hard to maintain.

这篇关于如何在SQL Server存储过程中的用户定义表类型上进行ForEach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆