在DB2 SQL中声明一个变量 [英] Declare a variable in DB2 SQL

查看:876
本文介绍了在DB2 SQL中声明一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在DB2中运行以下SQL Server代码吗?

Does anyone know how to run the following SQL Server code in DB2?

我正在转换SQL Server脚本,以便它们可以在DB2系统上运行,并且在围绕DB2中变量的使用方面遇到了一些问题。

I am converting SQL Server scripts so that they will run on a DB2 system and am having some problems wrapping my head around the use of variables in DB2.

T-SQL代码

这显然不是实际代码,但可以很好地作为示例

This is obviously not the actual code but works well as an example.

DECLARE @INPUT_VALUE INT
SET INPUT_VALUE = 4756

SELECT *
FROM TABLE1
WHERE TABLE1.COLUMN1 = @INPUT_VALUE


推荐答案

我想这个论坛发布(我在下面完整引用)应该

I imagine this forum posting, which I quote fully below, should answer the question.

在过程,函数或触发器定义中,或在动态SQL语句中(嵌入在宿主程序):

Inside a procedure, function, or trigger definition, or in a dynamic SQL statement (embedded in a host program):

BEGIN ATOMIC
 DECLARE example VARCHAR(15) ;
 SET example = 'welcome' ;
 SELECT *
 FROM   tablename
 WHERE  column1 = example ;
END

或(在任何环境中):

WITH t(example) AS (VALUES('welcome'))
SELECT *
FROM   tablename, t
WHERE  column1 = example

或(尽管这可能不是您想要的,因为变量只需创建一次,但此后所有人都可以使用,尽管其内容将基于每个用户都是私有的):

or (although this is probably not what you want, since the variable needs to be created just once, but can be used thereafter by everybody although its content will be private on a per-user basis):

CREATE VARIABLE example VARCHAR(15) ;
SET example = 'welcome' ;
SELECT *
FROM   tablename
WHERE  column1 = example ;

这篇关于在DB2 SQL中声明一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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