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

查看:30
本文介绍了在 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天全站免登陆