我想将这个EXISTS
查询的结果保存到变量中:
DECLARE @max_date DATETIME2
DECLARE @DatabaseSchema VARCHAR(MAX)
SET @DatabaseSchema = 'SALES_MART'
SET @max_date = CASE
WHEN EXISTS(SELECT MAX(SaleDate)
FROM @DatabaseSchema.SalesReport
WHERE Categoryname = 'Asia')
THEN (SELECT MAX(SaleDate)
FROM @DatabaseSchema.SalesReport
WHERE Categoryname = 'Asia')
ELSE GETDATE()
END
EXEC @max_date
执行它时出现错误
无效的列名@ DatabaseName.SalesReport
错误的原因是SQL Server在FROM
之后不接受变量。
我用Google搜索并试图删除“ CASE WHEN”,但仍然无法正常工作。 您能帮我这个疑问吗?我应该如何编辑?
这是我的测试表:
CREATE SCHEMA SALES_MART;
CREATE TABLE SALES_MART.SalesReport
(
id INT,
SaleDate DATETIME2,
sales INT,
Categoryname VARCHAR(100)
);
INSERT INTO SALES_MART.SalesReport
VALUES (1, '1905-07-08 00:00:00.000', 50, 'Asia'),
(2, '1905-07-08 00:00:00.000', 90, 'Asia'),
(3, '1905-07-08 00:00:00.000', 100, 'EU');
答案 0 :(得分:1)
假设您正在使用SSMS作为IDE,则可以简单地启用Sql Command Mode,然后获得一个动态模式值。尝试以下代码:
click
答案 1 :(得分:0)
这是已更正的代码,它将结果存储在变量@max_date中 最后选择是显示结果
CREATE SCHEMA SALES_MART;
go
create table SALES_MART.SaleReport (id int,SaleDate datetime2,sales int,Categoryname varchar(100));
INSERT INTO SALES_MART.SaleReport
VALUES
(1,'1905-07-08 00:00:00.000',50,'Asia'),
(2,'1905-07-08 00:00:00.000',90,'Asia'),
(3,'1905-07-08 00:00:00.000',100,'EU');
Declare @max_date datetime2
Declare @DatabaseSchema varchar(max)
SElect @max_date=CASE WHEN EXISTS(SELECT max(SaleDate)
FROM SALES_MART.SaleReport
WHERE Categoryname = 'Asia')
THEN (SELECT max(SaleDate)
FROM SALES_MART.SaleReport
WHERE Categoryname = 'Asia' )
ELSE getdate() END
select @max_date [result in varaiable]
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/ImC1o.png
**ANOTHER SOLUTION WITH DYNAMIC SCRIPT**
This solution can be used for many schemas, it inserts the result (with the schema name and max date) in Tbale mytable
CREATE SCHEMA SALES_MART;
go
create table SALES_MART.SaleReport (id int,SaleDate datetime2,sales int,Categoryname varchar(100));
INSERT INTO SALES_MART.SaleReport
VALUES
(1,'1905-07-08 00:00:00.000',50,'Asia'),
(2,'1905-07-08 00:00:00.000',90,'Asia'),
(3,'1905-07-08 00:00:00.000',100,'EU');
Declare @max_date datetime2
Declare @DatabaseSchema varchar(max)
set @DatabaseSchema='SALES_MART'
declare @script as varchar(max)
create table mytable(myschema varchar(50),maxdate date)
delete from mytable
set @script='
insert into mytable (myschema,maxdate)
select '''+@DatabaseSchema+''',
CASE WHEN EXISTS(SELECT max(SaleDate)
FROM '+@DatabaseSchema+'.SaleReport
WHERE Categoryname = ''Asia'')
THEN (SELECT max(SaleDate)
FROM SALES_MART.SaleReport
WHERE Categoryname = ''Asia'' )
ELSE getdate() END'
exec(@script)
select * from mytable
答案 2 :(得分:0)
如果您想为架构名称使用参数,则需要使用动态SQL。它将字符串转换为查询,因此您可以将字符串参数与编写的字符串查询连接起来。您也可以将值输出到参数。
我已通过合并简化了查询。如果不为NULL,则使用第一个值;如果第一个为NULL,则使用第二个值
Declare @DatabaseSchema varchar(max)
SET @DatabaseSchema ='SALES_MART'
DECLARE @MaxDate DATETIME
DECLARE @Query NVARCHAR(MAX) = 'SELECT COALESCE((SELECT max(SaleDate) FROM '+ @DatabaseSchema + '.SalesReport WHERE Categoryname = ''Asia''), GETDATE())'
EXECUTE sp_executesql @Query, N'@Output DATETIME OUT', @MaxDate OUT
PRINT @MaxDate
答案 3 :(得分:0)
变量。
在您的情况下,您试图在模式名称中使用它,要像这样使用它,将有两个选择。
选项1:
定义每个选择语句,并使用IF根据变量输入获取所需的选择语句。
示例:
DECLARE
@max_date DATETIME2
, @DatabaseSchema VARCHAR(max) = 'SALES_MART'
SET @max_date = CASE @DatabaseSchema
WHEN 'SALES_MART' THEN (SELECT MAX(SaleDate) FROM SALES_MART.SalesReport WHERE Categoryname = 'Asia')
WHEN 'SALES_MART_TWO' THEN (SELECT MAX(SaleDate) FROM SALES_MART_TWO.SalesReport WHERE Categoryname = 'Asia')
ELSE GETDATE()
END
SET @max_date = CASE WHEN @max_date IS NULL THEN GETDATE() ELSE @max_date END -- You can replace it with ISNULL(@max_date, GETDATE())
SELECT @max_date
选项2 (并非总是建议这样做,因为它有自己的风险):
改用daynamic sql。
DECLARE
@max_date DATETIME2
, @DatabaseSchema VARCHAR(max) = 'SALES_MART'
, @sql VARCHAR(max) = ''
SET @sql = 'ISNULL( (SELECT MAX(SaleDate) FROM @DatabaseSchema.SalesReport WHERE Categoryname = ''Asia''), GETDATE())'
SET @sql = REPLACE(@sql, '@DatabaseSchema', @DatabaseSchema)
EXEC (@sql)