Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Simple Archive Issue

Author  Topic 

cduance
Starting Member

6 Posts

Posted - 2010-09-07 : 16:47:09
I have a stored Procedure which is as follows

INSERT INTO Reporting.tblServerMonitor(ServerMonitorID, FK_tblServers_ServerID, ServerStatus, ServerTimeStamp)

SELECT ServerMonitorID, FK_tblServers_ServerID, ServerStatus, ServerTimeStamp
FROM ServerService.tblServerMonitor
WHERE ArchiveBit = 0

-- ====================================================
-- Reset ArchiveBit to 1 for Archived Records
-- ====================================================
UPDATE ServerService.tblServerMonitor
SET ArchiveBit = 1
WHERE ArchiveBit = 0

-- ====================================================
-- Update Server Monitor Record for ServerDriveID
-- ====================================================
UPDATE ServerService.tblServerMonitor
SET ServerStatus = 1, ServerTimeStamp = GETDATE(), ArchiveBit = 0
WHERE ServerMonitorID = 1


However, the only bit that seems to run is the update. An additional record is not copied to the Reporting table before the record is updated. If I run this stand alone then it works but being called from a web page only updates the record which is the final bit?

Any ideas.

Many thanks

Charles

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-07 : 16:53:16
If it works from an SSMS query window and not from your application, then the issue is with your application. Could you post the relevant application code?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cduance
Starting Member

6 Posts

Posted - 2010-09-07 : 16:57:13
Cheers

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strCmd As String = "ServerService.ServerMonitorUpdate"
Dim myConnStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ServerConnString").ConnectionString
Dim myConn As New SqlConnection(myConnStr)
Dim myColl As New NameValueCollection

myColl = Request.QueryString

Using mySQLCmd As New SqlCommand(strCmd, myConn)
myConn.Open()

mySQLCmd.CommandType = CommandType.StoredProcedure
mySQLCmd.CommandText = strCmd

mySQLCmd.Parameters.Add("@ServerMonitorID", SqlDbType.Int).Value = myColl("ServerID")

mySQLCmd.ExecuteNonQuery()
End Using

End Sub
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-07 : 17:12:33
I missed in your original post that is in a stored procedure. Could you post it?

Also, are you using MySql or Microsoft SQL Server?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cduance
Starting Member

6 Posts

Posted - 2010-09-07 : 17:17:20
The stored proc is the same as in my first post. I just deleted out the additional bits but here it is. Its MS SQL 2008


/****** Object: StoredProcedure [ServerService].[ServerMonitorUpdate]

Script Date: 09/07/2010 21:25:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





-- ======================================================
-- Author: Me
-- Create date: 19/06/2010
-- Description: Updates the Server record in the ServerMonitor table
-- Change: Added Archive Procedure on 24/08/2010
-- ======================================================
ALTER PROCEDURE [ServerService].[ServerMonitorUpdate]
-- Add the parameters for the stored procedure here
@ServerMonitorID Int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
--SET NOCOUNT ON;

-- ====================================================
-- Archive Records with ArchiveBit = 0
-- ====================================================
INSERT INTO Reporting.tblServerMonitor(ServerMonitorID, FK_tblServers_ServerID, ServerStatus, ServerTimeStamp)

(SELECT ServerMonitorID, FK_tblServers_ServerID, ServerStatus, ServerTimeStamp
FROM ServerService.tblServerMonitor
WHERE ArchiveBit = 0)

-- ====================================================
-- Reset ArchiveBit to 1 for Archived Records
-- ====================================================
UPDATE ServerService.tblServerMonitor
SET ArchiveBit = 1
WHERE ArchiveBit = 0
-- ====================================================
-- Update Server Monitor Record for ServerDriveID
-- ====================================================
UPDATE ServerService.tblServerMonitor
SET ServerStatus = 1, ServerTimeStamp = GETDATE(), ArchiveBit = 0
WHERE ServerMonitorID = @ServerMonitorID


END


GO


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-07 : 17:20:43
I can't spot any issues with any of the code you've posted. Perhaps @ServerMonitorID isn't being passed the right value? Could you run SQL Profiler with RPC:Completed and SP:Completed events to capture the stored procedure call from the application and also from a query window? Do the Completed events show the same call?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cduance
Starting Member

6 Posts

Posted - 2010-09-07 : 17:28:41
The record is being updated so it is passing the correct serverID the problem seems to be that it does not do either of the two queries just the last one which seems very weird. I don't know if I need to get the SProc to do each query seperately?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-07 : 17:38:58
No a sproc can handle many different things.

I'm out of ideas although SQL Profiler would be very beneficial still.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

cduance
Starting Member

6 Posts

Posted - 2010-09-07 : 17:49:43
OK thanks for your help. Ill let you know when I find the issue
Go to Top of Page

cduance
Starting Member

6 Posts

Posted - 2010-09-08 : 14:51:38
Permissions :(

Due to taking data from one table in schema A and trying to insert into schema B
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-08 : 15:00:34
Glad you found it, and thanks for posting back with the solution!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-09-09 : 02:34:10
quote:
Originally posted by cduance

Permissions :(

Due to taking data from one table in schema A and trying to insert into schema B



Did SQL not raise an error? (Or perhaps the APP didn't catch it )
Go to Top of Page
   

- Advertisement -