托管在变量上的查询中的 INSERT INTO SELECT 出错
Error on INSERT INTO SELECT in query hosted on variable
有点迷茫
谁能告诉我为什么我在下一个脚本中出现 sintaxis 错误?
declare @schema1 NVARCHAR(100)
set @schema1 = 'ex.:DATABASENAME'
declare @query NVARCHAR(500)
set @query = 'INSERT UsersSessions (UserSessionId, IpAddress, ChannelId, IsEmployee, UserId, ClientFullName, UserAgent, StartDate, ExpirationDate, SessionDuration)
SELECT us.UserSessionId, us.IpAddress, us.ChannelId, CASE WHEN us.ChannelId = 2 THEN 1 ELSE 0 END AS IsEmployee, us.UserId, (u.Name + u.LastName) as ClientFullName, us.UserAgent, us.StartDate, us.ExpirationDate, (us.ExpirationDate-us.StartDate) AS SessionDuration
FROM ' + @schema1 + '.UsersSessions us INNER JOIN ' + @schema1 + '.Users u ON us.UserId = u.UserId WHERE us.UserSessionId NOT IN (SELECT UserSessionId FROM UsersSessions)'
EXEC(@query)
结果:
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'Use'.
Completion time: 2021-11-16T11:10:32.6309920-03:00
如果我删除插入块,我会得到脚本 运行
declare @schema1 NVARCHAR(100)
set @schema1 = 'ex.:DATABASENAME'
declare @query NVARCHAR(500)
set @query = '
SELECT us.UserSessionId, us.IpAddress, us.ChannelId, CASE WHEN us.ChannelId = 2 THEN 1 ELSE 0 END AS IsEmployee, us.UserId, (u.Name + u.LastName) as ClientFullName, us.UserAgent, us.StartDate, us.ExpirationDate, (us.ExpirationDate-us.StartDate) AS SessionDuration
FROM ' + @schema1 + '.UsersSessions us INNER JOIN ' + @schema1 + '.Users u ON us.UserId = u.UserId WHERE us.UserSessionId NOT IN (SELECT UserSessionId FROM UsersSessions)'
EXEC(@query)
结果:
(1 row affected)
Completion time: 2021-11-16T11:11:07.6741062-03:00
我认为缺少某些字符?
感谢和问候!
您的查询变量长度不够,修改如下。另外,模式名称只能接受某些字符,我猜冒号是不允许的。
declare @query NVARCHAR(max)
有点迷茫
谁能告诉我为什么我在下一个脚本中出现 sintaxis 错误?
declare @schema1 NVARCHAR(100)
set @schema1 = 'ex.:DATABASENAME'
declare @query NVARCHAR(500)
set @query = 'INSERT UsersSessions (UserSessionId, IpAddress, ChannelId, IsEmployee, UserId, ClientFullName, UserAgent, StartDate, ExpirationDate, SessionDuration)
SELECT us.UserSessionId, us.IpAddress, us.ChannelId, CASE WHEN us.ChannelId = 2 THEN 1 ELSE 0 END AS IsEmployee, us.UserId, (u.Name + u.LastName) as ClientFullName, us.UserAgent, us.StartDate, us.ExpirationDate, (us.ExpirationDate-us.StartDate) AS SessionDuration
FROM ' + @schema1 + '.UsersSessions us INNER JOIN ' + @schema1 + '.Users u ON us.UserId = u.UserId WHERE us.UserSessionId NOT IN (SELECT UserSessionId FROM UsersSessions)'
EXEC(@query)
结果:
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'Use'.
Completion time: 2021-11-16T11:10:32.6309920-03:00
如果我删除插入块,我会得到脚本 运行
declare @schema1 NVARCHAR(100)
set @schema1 = 'ex.:DATABASENAME'
declare @query NVARCHAR(500)
set @query = '
SELECT us.UserSessionId, us.IpAddress, us.ChannelId, CASE WHEN us.ChannelId = 2 THEN 1 ELSE 0 END AS IsEmployee, us.UserId, (u.Name + u.LastName) as ClientFullName, us.UserAgent, us.StartDate, us.ExpirationDate, (us.ExpirationDate-us.StartDate) AS SessionDuration
FROM ' + @schema1 + '.UsersSessions us INNER JOIN ' + @schema1 + '.Users u ON us.UserId = u.UserId WHERE us.UserSessionId NOT IN (SELECT UserSessionId FROM UsersSessions)'
EXEC(@query)
结果:
(1 row affected)
Completion time: 2021-11-16T11:11:07.6741062-03:00
我认为缺少某些字符?
感谢和问候!
您的查询变量长度不够,修改如下。另外,模式名称只能接受某些字符,我猜冒号是不允许的。
declare @query NVARCHAR(max)