Click here to Skip to main content
15,890,438 members
Home / Discussions / Database
   

Database

 
AnswerRe: TableAdapters results differ from Stored Procedure results, C# - SQL 2000 Pin
Michael Potter11-Sep-12 4:06
Michael Potter11-Sep-12 4:06 
GeneralRe: TableAdapters results differ from Stored Procedure results, C# - SQL 2000 Pin
lowhitaker11-Sep-12 4:31
lowhitaker11-Sep-12 4:31 
QuestionHow do i solved Timeout Expired Error in ASP.net with Backend as SQL 2008 Pin
Pratibha Mhatre7-Sep-12 2:01
Pratibha Mhatre7-Sep-12 2:01 
QuestionRe: How do i solved Timeout Expired Error in ASP.net with Backend as SQL 2008 Pin
Eddy Vluggen7-Sep-12 2:53
professionalEddy Vluggen7-Sep-12 2:53 
Answerclose and dispose Pin
David Mujica7-Sep-12 5:10
David Mujica7-Sep-12 5:10 
GeneralRe: close and dispose Pin
Pratibha Mhatre19-Sep-12 20:05
Pratibha Mhatre19-Sep-12 20:05 
GeneralRe: close and dispose Pin
David Mujica20-Sep-12 10:22
David Mujica20-Sep-12 10:22 
QuestionSequence of events inside a stored procedure Pin
Clive D. Pottinger6-Sep-12 8:13
Clive D. Pottinger6-Sep-12 8:13 
Hello everyone.

Everything I have read says that operations within a stored procedure are executed in the order in which they are encountered, and the semi-colons and BEGIN-END blocks can be used to ensure that statements are executed before subsequent statements are performed.

However I am stumped by what is happening in my script. The intent of the script is to copy an order record and all associated table entries for the order from one database to another. I have stripped down the script code below to just the order table itself (and removed some passwords, server names etc).

Here is the code:
IF @@SERVERNAME <> 'Server1'
  BEGIN
      EXEC SP_ADDLINKEDSERVER
        'Server1'

      EXEC SP_ADDLINKEDSRVLOGIN
        'Server1',
        'False',
        NULL,
        'userX',
        'passwordX'
  END

go

DECLARE @mode VARCHAR(1);
DECLARE @orderNum INT;

SET @orderNum = 1338464;
SET @mode = 'R' -- set to 'R' for Report or 'U' for update
BEGIN TRANSACTION

DECLARE @sourceName VARCHAR(8);
DECLARE @okay CHAR(1);
DECLARE @user VARCHAR(20);

SET @sourceName = 'liveDB';
SET @user = SYSTEM_USER;

CREATE synonym sourcesummary FOR [Server1].[liveDB].[dbo].[order_summary];
SET @okay = 'Y'

IF @okay = 'Y'
   AND @@SERVERNAME = 'Server1'
  BEGIN
      SELECT 'Cannot execute on live server - retry on the sever hosting the db that is to be copied to' [error]

      SET @okay = 'N'
  END

IF @okay = 'Y'
   AND NOT EXISTS (SELECT order_num
                   FROM   sourcesummary
                   WHERE  order_num = @orderNum)
  BEGIN
      SELECT 'Unable to find order' [error],@sourceName [source db],@orderNum [order number];

      SET @okay = 'N';
  END

IF @okay = 'Y'
  BEGIN
      IF EXISTS (SELECT order_num
                 FROM   order_summary
                 WHERE  ordr_num = @orderNum)
        BEGIN
            PRINT 'deleting order_summary:';

            DELETE order_summary
            WHERE  order_num = @orderNum;
        END

      BEGIN

	  PRINT 'copying order_summary:';
          INSERT INTO order_summary
          SELECT [order_num],[order_date], etc, etc, etc...
          FROM   sourcesummary
          WHERE  sourcesummary.order_num = @orderNum;
      END

	  PRINT 'retreiving copied order_summary:';
      SELECT 'copied' [order_summary],*
      FROM   order_summary
      WHERE  order_num = @orderNum;
  END

DROP synonym sourcesummary;

IF @mode = 'U'
  BEGIN
      COMMIT TRANSACTION

      SELECT 'Changes have been applied to the database' [notice]
  END
ELSE
  BEGIN
      ROLLBACK TRANSACTION

      SELECT 'All changes have been rolled back' [notice]
  END 

And here is the result from MSSQL's Messages window:
Msg 15028, Level 16, State 1, Procedure sp_MSaddserver_internal, Line 89
The server 'Server1' already exists.
deleting order_summary:

(1 row(s) affected)
copying order_summary:

(1 row(s) affected)
retreiving copied order_summary:

(0 row(s) affected)

(1 row(s) affected)


The error at the top of the output (re Server1 already exists) I understand and that's okay.

However, notice that copying order_summary says that 1 row was affected, but the very next operation to display that row obtains no records. This is what I don't understand, and I can't move forward until I get this row to exist in the db.

Can anyone see some blatantly newbie faux-pas that is hidden to my blatantly newbie eyes?

Please?
Clive Pottinger
Victoria, BC


modified 6-Sep-12 14:28pm.

AnswerRe: Sequence of events inside a stored procedure Pin
Andrei Straut6-Sep-12 9:23
Andrei Straut6-Sep-12 9:23 
AnswerRe: Sequence of events inside a stored procedure Pin
PIEBALDconsult6-Sep-12 9:49
mvePIEBALDconsult6-Sep-12 9:49 
AnswerRe: Sequence of events inside a stored procedure Pin
Clive D. Pottinger7-Sep-12 6:42
Clive D. Pottinger7-Sep-12 6:42 
JokeRe: Sequence of events inside a stored procedure Pin
cbeglobal12-Sep-12 1:44
cbeglobal12-Sep-12 1:44 
Questioni m new in sql.i want print a message(the table employe is not exist in database) in error handling with try and catch Pin
Rashid Choudhary6-Sep-12 2:02
Rashid Choudhary6-Sep-12 2:02 
AnswerRe: i m new in sql.i want print a message(the table employe is not exist in database) in error handling with try and catch Pin
Wes Aday6-Sep-12 2:26
professionalWes Aday6-Sep-12 2:26 
AnswerRe: i m new in sql.i want print a message(the table employe is not exist in database) in error handling with try and catch Pin
Eddy Vluggen6-Sep-12 2:28
professionalEddy Vluggen6-Sep-12 2:28 
AnswerRe: i m new in sql.i want print a message(the table employe is not exist in database) in error handling with try and catch Pin
PIEBALDconsult6-Sep-12 3:33
mvePIEBALDconsult6-Sep-12 3:33 
QuestionRetrieve records from sqlserver 2005 which are today inserted Pin
sali225-Sep-12 15:23
sali225-Sep-12 15:23 
AnswerRe: Retrieve records from sqlserver 2005 which are today inserted Pin
PIEBALDconsult5-Sep-12 17:17
mvePIEBALDconsult5-Sep-12 17:17 
AnswerRe: Retrieve records from sqlserver 2005 which are today inserted Pin
sali225-Sep-12 17:33
sali225-Sep-12 17:33 
GeneralRe: Retrieve records from sqlserver 2005 which are today inserted Pin
PIEBALDconsult5-Sep-12 18:07
mvePIEBALDconsult5-Sep-12 18:07 
AnswerMessage Closed Pin
5-Sep-12 22:37
Santhosh Kumar Jayaraman5-Sep-12 22:37 
GeneralRe: Retrieve records from sqlserver 2005 which are today inserted Pin
PIEBALDconsult6-Sep-12 4:51
mvePIEBALDconsult6-Sep-12 4:51 
QuestionHow to join two sql queries together? Pin
turbosupramk35-Sep-12 11:09
turbosupramk35-Sep-12 11:09 
AnswerRe: How to join two sql queries together? Pin
Mycroft Holmes5-Sep-12 12:49
professionalMycroft Holmes5-Sep-12 12:49 
AnswerRe: How to join two sql queries together? Pin
Osama Bin Laden 20125-Sep-12 15:02
Osama Bin Laden 20125-Sep-12 15:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.