简体   繁体   English

如何从商店程序中获取表结果?

[英]How to get the Table result from the store procedure?

I have tried to write the store procedure with some condition check inside the Where syntax. 我试图在Where语法中使用一些条件检查来编写存储过程。

But it only shows the status of the query instead of result. 但它只显示查询的状态而不是结果。

Could any body help to resolve this issue. 任何机构都可以帮助解决这个问题。

Please find the query 请查找查询

declare @query varchar(200)
declare @option varchar(10) = 'A'
set @query ='select * from TableName'+
(case when @query = ''
then
'where ColumnName = 1 '
end)
execute sp_sqlexec @query

Your query should be 你的查询应该是

declare @query varchar(200)
declare @option varchar(10) ='A'
declare @search as int=1 

select @query ='select * from TableName '+
case when @option ='A'
then
' where ColumnName = '+ cast(@search as varchar(200))
else ''
end
execute sp_sqlexec @query

see demo 看看演示

What i can see for sure is lack of space before where condition. 我可以肯定的是,在有条件之前缺乏空间。

declare @query varchar(200)
declare @option varchar(10) = 'A'
set @query ='select * from TableName'+
(case when @query = ''
then
' where ColumnName = 1 '
end)

You can try like this : 你可以尝试这样:

declare @query varchar(200)

declare @option varchar(10) = 'A'

set @query ='select * from TableName '

if @option ='A'
   set @query = @query + ' where ColumnName = 1 '

--print @query

execute sp_sqlexec @query
declare     @query  nvarchar(max)
        ,   @option varchar(10)     = 'A';

set @query = N'select * from TableName '
           + CASE WHEN @option IS NOT NULL
                THEN N' where ColumnName = 1 ' ELSE N'' END

execute sp_executesql @query;

Avoid using sp_sqlexec it is very old and not supported anymore, use sp_executesql instead. 避免使用sp_sqlexec它很旧并且不再受支持,请改用sp_executesql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM