我无法确定如何根据另一个表中的值替换存储过程结果的值。我有一个表格[LOG]格式如下:
TIME STAMP, TAG, DESCRIPTION, EVENTCODE, SUBEVENTCODE
30-Aug-2013 10:14:10, TAG X, HI TEMP FAULT, 3, 16
30-Aug-2013 10:12:10, TAG Y, HI PRESS FAULT, 3, 16
...
另一个表[EVENTS]基本上描述了EVENTCODE的内容:
EVENT, DESCRIPTION
1, FAULT
2, LOGIC
3, ALARM
我希望存储过程检索第一个表的2000个条目(行),而不是将EVENTCODE显示为数字,而是显示结果中第二个表中包含的描述。 e.g:
TIME STAMP, TAG, DESCRIPTION, EVENTCODE, SUBEVENTCODE
30-Aug-2013 10:14:10, TAG X, HI TEMP FAULT, ALARM, 16
原因是我有另一个与存储过程的结果交互的软件,并且不想创建另一个表来在数据库中保存这些结果。
以下是目前存储过程的样子:
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Get2kEvents]
AS
BEGIN
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
[DESCRIPTION],
[EVENTCODE],
[SUBEVENTCODE]
FROM [Arc_DB].[dbo].[LOG]
ORDER BY TIME_STAMP DESC
END
GO
感谢您的协助。很抱歉,如果这太基础了,但我在浏览本网站和其他网站时无法找到解决方案。
干杯。
TM
答案 0 :(得分:0)
您希望join
这两个表格。即:
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
[DESCRIPTION],
Events.Description,
[SUBEVENTCODE]
FROM [Arc_DB].[dbo].[LOG]
inner join events on log.eventcode = events.event
ORDER BY TIME_STAMP DESC
答案 1 :(得分:0)
使用JOIN
:
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
L.[DESCRIPTION],
E.[DESCRIPTION],
[SUBEVENTCODE]
FROM [Arc_DB].[dbo].[LOG] L
INNER JOIN [Arc_DB].[dbo].[EVENTS] E ON E.EVENT = L.EVENTCODE
ORDER BY TIME_STAMP DESC
答案 2 :(得分:0)
我会使用左连接并检查缺少的代码:
SELECT TOP 2000 CAST(datepart(day,TIME_STAMP) as char(2)) + '-' +
CAST(datename(month,TIME_STAMP) as char(3)) + '-' +
CAST(datepart(year,TIME_STAMP) as char(4))+ ' ' + CONVERT(varchar,TIME_STAMP,108)
as 'TIME STAMP',
[TAG],
[DESCRIPTION],
CASE WHEN EVENTS.DESCRIPTION IS NULL
THEN 'UNKNOWN CODE '+CAST(LOG.EVENTCODE AS VARCHAR(20))
ELSE EVENTS.DESCRIPTION
END AS [Event Type],
[EVENTCODE],
[SUBEVENTCODE]
FROM LOG
LEFT JOIN EVENTS ON EVENTS.EVENT= LOG.EVENTCODE
ORDER BY TIME_STAMP DESC