简体   繁体   English

将多个查询合并为一个查询(在SQL Server中,是否带有变量?)

[英]combining a multiple queries into a single query (in SQL Server, with variables?)

I'd like to apply the following query to two other circ_slip values ('payment' and 'refund') for a large number of locations. 我想将以下查询应用于大量地点的另外两个circ_slip值(“付款”和“退款”)。

delete from circ_slip_field 
where location = 'NEW LOCATION' and circ_slip = 'waive'
go
declare @copy_loc varchar(7), @new_loc varchar(7) 
select @copy_loc = 'OLD LOCATION' 
select @new_loc = 'NEW LOCATION'
insert circ_slip_field
       (circ_slip,location,section,ord,circ_slip_field_type,label,field_column,
        append_to_prev,justify,max_field_len,min_field_len,data_mask)
select  circ_slip,@new_loc,section,ord,circ_slip_field_type,label,field_column,
        append_to_prev,justify,max_field_len,min_field_len,data_mask
from circ_slip_field 
where circ_slip = 'waive' and location = @copy_loc

So I added the following line to the beginning and then copied the original query three times, replacing the two instances of 'waive' with 'payment' and 'refund' respectively. 因此,我将以下行添加到开头,然后复制了原始查询三遍,分别用“支付”和“退款”替换了“放弃”的两个实例。

 declare @copy_loc varchar(7), @new_loc varchar(7) 
 select @copy_loc = 'COPY LOCATION' 
 select @new_loc = 'NEW LOCATION'

I also removed the two select variable statements (@copy_loc and @new_loc) since they have already been declared. 我还删除了两个选择变量语句(@copy_loc和@new_loc),因为它们已被声明。

Any thoughts on why this did not work for me? 有什么想法为什么对我不起作用? I would be greatly appreciative. 我将不胜感激。 Needless to say I'm quite new to SQL in general. 不用说,我一般对SQL还是很陌生。

** EDIT: by "did not work" I meant the query ran without errors but did not make any changes to the NEW LOCATION. **编辑:“没有工作”是指查询运行没有错误,但未对“新位置”进行任何更改。

* 2nd EDIT: I think the issue may be with the "go" command. *第二编辑:我认为问题可能出在“执行”命令上。 In order to have the variables work throughout I need to remove the 'go' from my three replicated queries. 为了使变量始终有效,我需要从三个重复的查询中删除“ go”。

Notice @copy_loc and @new_loc are defined as varchar(7), but you're putting 12 characters into them. 注意,@copy_loc和@new_loc被定义为varchar(7),但是您要在其中输入12个字符。 They're truncated and won't match the fields in the table. 它们被截断,将与表中的字段不匹配。 Make the variables varchar(50). 使变量varchar(50)。 Don't worry, it won't use more space than it needs. 不用担心,它不会使用比所需更多的空间。

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

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