如何执行存储过程中变量中存储的查询 [英] How to Execute a Query Stored in a variable in Stored Procedure

查看:86
本文介绍了如何执行存储过程中变量中存储的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要查询存储在变量中的查询。 Query用于获取变量的给定值。我尝试使用exec命令但返回错误。我不知道怎么做

下面给出了SQL命令

这行用于生成所需的查询并存储到变量@mnvcSQL

select @mnvcSQL =''Select @ Prev = year''+ convert(varchar,cast((@ Field-1)as int))+''来自tblFinancialDistribution,其中ProjectId = ''+ cast(@pintProjectId as Varchar)+''和[lineno] =''+ cast(@mvcLineNoNetCashFlow as Varchar)

执行上面的行后,变量@mnvcSQL包含像SQL Sentance一样

从tblFinancialDistribution选择@ Prev = year4,其中ProjectId = 42并且[lineno] = - 29

我想要对此进行说明声明并希望在@Prev中得到结果值

I want to exicute a Query that stored in a variable. and the Query is used to get a given value to a variable. I tried to use exec command but that returns an error. i cant get an idea for how to do
the SQL commands are given bellow

This line is used to generate the required query and stored to a variable @mnvcSQL
select @mnvcSQL=''Select @Prev=year'' + convert(varchar,cast((@Field-1) as int))+ '' from tblFinancialDistribution where ProjectId='' + cast(@pintProjectId as Varchar)+ '' and [lineno]='' + cast(@mvcLineNoNetCashFlow as Varchar)

After Executing the above line the variable @mnvcSQL contain the the SQL Sentance like
Select @Prev=year4 from tblFinancialDistribution where ProjectId=42 and [lineno]=-29

I Want to exicute this statement and want the resulted value in @Prev

推荐答案

我认为你不能从exec命令返回参数。

但是当我仔细查看你的查询时,我不认为需要连接这个语句以便稍后执行。

是否可以按原样使用它并让@Prev以正常方式返回?


[PHP]选择@Prev =年+(@Field - 1)

来自tblFinancialDistribution,其中ProjectId = @pintProjectId

和[lineno] = @mvcLineNoNetCashFlow [/ PHP]
I don''t think you can return parameter from exec command.
But as I looked at your query closely I don?t see a need to concatenate this statement to be later executed.
Is it possible to use it as is and have @Prev returned its normal way?

[PHP]Select @Prev = year + (@Field - 1)
from tblFinancialDistribution where ProjectId = @pintProjectId
and [lineno]= @mvcLineNoNetCashFlow[/PHP]


我正在创建这个查询可以满足我的条件。里面的问题是这个。我想选择一个名字以年份开头的字段。我的表中列出了第1年到第15年的列。有一段时间我想要year1,有些时候我想要year7。那是动态的。唯一的解决方案是巩固声明。

,最终查询存储在变量中。该查询返回一个值。我想要查询查询并将结果输入另一个变量。
i am creating this query for stisfy my condition. the problem inside is this. i want to select a field thats name started with year. the year 1 to year 15 columns are in my table. some time i want year1 and some time i want year7. that is dynamic. the only solution is to concatinate the statement.
and the final query is stored in a variable. that query returns a value. i want to exicute the query and get the result in another variable.


上面的语句将year视为列名,@ field1是必须添加到year的值的变量。所以请检查一下,帮帮我
the above statement consider year as the column name and @field1 is the variable that must be added to the value of year. so pls check it, help me


这篇关于如何执行存储过程中变量中存储的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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