这是我第一次创建存储过程而且我不知道我做错了什么。
我收到以下错误:
Msg 7202,Level 11,State 2,Procedure update_call_details,Line 8
无法找到服务器' SELECT TOP 1 b'在sys.servers中。验证是否指定了正确的服务器名称。如有必要,执行存储过程sp_addlinkedserver以将服务器添加到sys.servers。
存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[update_call_details]
AS
BEGIN
DECLARE @EventTime varchar(50)
SET @EventTime = 'SELECT TOP 1 b.event_time FROM db1.dbo.TableName as b order by b.event_time desc'
EXEC @EventTime
INSERT INTO Sdb1.dbo.TableName
SELECT
CAST(START_TIME as DATE) as START_TIME,
AGENT_ID,
GRP_DBID,
DIRECTION,
ANI, DNIS, TRACKNUM,
N_HOLD, T_HOLD,
N_CONFRENCE, T_CONFRENCE,
N_TRANSFER, T_TRANSFER,
T_WRAP_UP,
TALK_TIME,
CAST(END_TIME as DATE) as END_TIME,
CAST(EVENT_TIME as DATE) as EVENT_TIME
FROM
OPENQUERY(db2, 'SELECT * FROM db2.TableName
WHERE EVENT_TIME > "@EventTime"')
END
答案 0 :(得分:1)
更改行: -
DECLARE @EventTime varchar(50)
到
DECLARE @EventTime varchar(200)
变量的大小正在截断其内容,因此EXEC命令失败。我已经增加了变量的大小,希望能够涵盖您可能做出的任何其他更改。
答案 1 :(得分:1)
您无法使用openquery
之类的参数。您的查询必须是单个字符串文字。
在这种情况下,您只需将EventTime
子查询直接放入内联:
ALTER PROCEDURE [dbo].[update_call_details]
AS
BEGIN
INSERT INTO Sdb1.dbo.TableName
SELECT CAST(START_TIME as DATE) as START_TIME,
AGENT_ID,
GRP_DBID,
DIRECTION,
ANI,
DNIS,
TRACKNUM,
N_HOLD,
T_HOLD,
N_CONFRENCE,
T_CONFRENCE,
N_TRANSFER,
T_TRANSFER,
T_WRAP_UP,
TALK_TIME,
CAST(END_TIME as DATE) as END_TIME,
CAST(EVENT_TIME as DATE) as EVENT_TIME
FROM openquery(db2,
'SELECT *
FROM db2.TableName
WHERE EVENT_TIME >
(SELECT TOP 1 b.event_time
FROM db1.dbo.TableName as b
order by b.event_time desc)')
END
否则......要在查询中使用参数,您可以使用此问题中的一个选项:including parameters in OPENQUERY