我在表中有以下列: IdRec dataora aparat1 aparat2 .... aparat100 和存储过程中的以下代码:
ALTER PROCEDURE [dbo].[GetConsumRealElQRo]
@id_aparat int,
@datastart nvarchar(20),
@datastop nvarchar(20)
AS
declare
@val_max real,
@data_min datetime,
@data_max datetime,
@val_data_min real,
@val_data_max real,
@consum real
BEGIN
SET NOCOUNT ON;
if @id_aparat=1
begin
select @val_max=max(aparat1),@data_min=min(dataora),@data_max=max(dataora) from table_name where (dataora between convert(datetime,@datastart,101) and convert(datetime,@datastop,101)) and aparat1 is not null
set @val_data_min=(
select aparat1 from table_name
where dataora=@data_min)
set @val_data_max=(
select aparat1 from table_name
where dataora=@data_max)
if @val_data_max<>@val_max
begin
set @consum=@val_max-@val_data_min+@val_data_max
end
else
begin
set @consum=@val_data_max-@val_data_min
end
select @consum
end
if @id_aparat=2
begin
select @val_max=max(aparat2),@data_min=min(dataora),@data_max=max(dataora) from table_name where (dataora between convert(datetime,@datastart,101) and convert(datetime,@datastop,101)) and aparat2 is not null
set @val_data_min=(
select aparat2 from table_name
where dataora=@data_min)
set @val_data_max=(
select aparat2 from table_name
where dataora=@data_max)
if @val_data_max<>@val_max
begin
set @consum=@val_max-@val_data_min+@val_data_max
end
else
begin
set @consum=@val_data_max-@val_data_min
end
select @consum
end
依此类推,对于这 100 个名为 aparat1、aparat2...aparat100 的列中的每一列。
因此,该过程接收一个整数列标识符和两个日期作为参数,进行一些计算并返回一个值。列标识符标识列并给出列名(即“aparat”+@id_aparat)。对于@id_aparat 的每个值,我想要的只是将计算块放在for 语句中,而不是复制它一百次。这可以使用动态 SQL 完成吗?
编辑:工作代码 问题已解决。对于任何有兴趣的人,这里是工作代码:
USE [2013date]
GO
/****** Object: StoredProcedure [dbo].[GetConsumRealElQRo] Script Date: 4/18/2013 2:39:19 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetConsumRealElQRo]
@id_aparat int,
@datastart nvarchar(20),
@datastop nvarchar(20)
AS
declare
@val_max real,
@data_min datetime,
@data_max datetime,
@val_data_min real,
@val_data_max real,
@consum real
BEGIN
SET NOCOUNT ON;
declare
@aparat as nvarchar(10),
@tabel as nvarchar(50),
@query as nvarchar(1000)
set @aparat = 'aparat' + CONVERT(nvarchar(3),@id_aparat)
set @tabel = 'table_name'
set @query = 'select @val_max=max(' + @aparat + '),@data_min=min(dataora),@data_max=max(dataora) from ' + @tabel + '
where (dataora between convert(datetime,''' + @datastart + ''',101) and convert(datetime,''' + @datastop + ''',101)) and ' + @aparat + ' is not null'
--select @query
exec sp_executesql @query, N'@val_max real output, @data_min datetime output, @data_max datetime output', @val_max output, @data_min output, @data_max output
--select @val_max
--select @data_min
--select @data_max
set @query = 'set @val_data_min=(select ' + @aparat + ' from ' + @tabel + ' where dataora=''' + Convert(nvarchar(30),@data_min,121) + ''')'
--select @query
exec sp_executesql @query, N'@val_data_min real output', @val_data_min output
--select @val_data_min
set @query = 'set @val_data_max=(select ' + @aparat + ' from ' + @tabel + ' where dataora=''' + Convert(nvarchar(30),@data_max,121) + ''')'
--select @query
exec sp_executesql @query, N'@val_data_max real output', @val_data_max output
--select @val_data_max
if @val_data_max<>@val_max
begin
set @consum=@val_max-@val_data_min+@val_data_max
end
else
begin
set @consum=@val_data_max-@val_data_min
end
select @consum
END