【发布时间】:2012-01-05 20:04:56
【问题描述】:
我设置了 MSSQL Server 2008 (Express)。 在我的数据库中,我有一组表和一个存储过程。
我想要实现的是获取对现有表所做的任何更改,并在过程结束时返回它们。我创建的存储过程在 MSSQL Management Studio 中本地运行时运行良好。
但是,当我通过 JDBC 连接调用该过程时,该过程的某些部分似乎还没有完成。
我正在尝试做的总结如下:
1) Put a snapshot of the data contained in CurrentTableA into #CurrentShotA (temporary table)
2) Compare #CurrentShotA with PreviousTableA
3) Insert differences into #TempTableB
(this equates to new rows or altered rows in #CurrentShotA)
4) Empty PreviousTableA
5) Insert contents of #CurrentShotA into PreviousTableA
6) Select * from #TempTableB (return all new rows and changes)
第 6 步在第一次通过 JDBC 调用时正确返回数据。 当第二次和后续调用该过程时,很明显步骤 5 没有按预期完成。当 PreviousTableA 应包含旧数据的快照时,它始终为空。
问题是为什么该过程在 MSSQL Management Studio 中调用时可以正常工作,但在我通过 JDBC 调用时不能正常工作?
CREATE PROCEDURE [dbo].[getUpdatedSchedules]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Check of the temporary table exists, delete if it does
IF OBJECT_ID('#TempTableB','U')IS NOT NULL
begin
drop table #TempTableB
end
-- Force the creation of the temporary tables quickly
select * into #TempTableB from dbo.CurrentTableA where 1=0
select * into #CurrentShotA from dbo.CurrentTableA where 1=0
-- Get the differences between schedules and put into #TempTableB
insert #CurrentShotA select * from dbo.CurrentTableA
insert #TempTableB select * from #CurrentShotA
except select * from dbo.PreviousTableA
TRUNCATE TABLE dbo.PreviousTableA
insert dbo.PreviousTableA select * from #CurrentShotA
select * from #TempTableB
END
GO
我对存储过程和 MSSQL 配置还不够新,所以我认为这可能是权限问题。我使用未链接到 Windows 帐户的 SQL 身份验证登录到 MSSQL Studio,并且该过程正常运行,因此我认为它不是权限。
我希望我的解释和问题足够清楚。对于我做错的任何想法或建议,我将不胜感激。
【问题讨论】:
-
对问题没有帮助,但假设没有 scheduleID TRUNCATE TABLE dbo.PreviousTableA 而不是
delete from dbo.PreviousTableA where scheduleID > -1。 -
啊,太好了,那在我的待办事项清单上。谢谢。
-
我已经在数据库中创建了一个包含数据副本的实际表,我根据存储过程中的时间戳命名它。当我从 JDBC 调用我的过程时,表中的数据被返回,但是当我去查看 SSMS 时,表不存在。我通过 SELECT * INTO [tableName_timestamp] from currentTableA 创建它
标签: sql-server stored-procedures jdbc ssms temp-tables