如何执行存储过程中变量中存储的查询

编程入门 行业动态 更新时间:2024-10-26 00:18:34
本文介绍了如何执行存储过程中变量中存储的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要查询存储在变量中的查询。 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

更多推荐

如何执行存储过程中变量中存储的查询

本文发布于:2023-10-26 09:26:04,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   过程中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!