Question : Deadly Script?

Experts,

Every time I run the attached script, I can no longer query the Agents table, as if its index is fried or something.  Is there anything you can see in this script that you think would cause this problem?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
--Insert Agent into UAT Central and appropriate Client databases.
DECLARE @FirstName varchar (50)
DECLARE @LastName varchar (50)
DECLARE @Total int
DECLARE @MolinaAccess int
DECLARE @EmblemAccess int
DECLARE @NewAgentID int
DECLARE @Error int
 
SET @Total=0
SET @Error=-1
 
SET @FirstName='Peter'
SET @LastName='Buettner'
 
SET @MolinaAccess=1
SET @EmblemAccess=1
 
BEGIN TRANSACTION 
 
USE CCS_Central
SELECT @Total=COUNT(*) FROM Agent 
WHERE FirstName=@FirstName 
AND LastName=@LastName
 
PRINT @Total
 
 
If @Total > 0 
BEGIN
	PRINT @FirstName + ' ' + @LastName + ' already exists in Agent table.'
END
 
 
SELECT @Total=COUNT(*) FROM AgentClient  
WHERE AgentID IN
(
SELECT AgentID FROM Agent 
WHERE FirstName=@FirstName 
AND LastName=@LastName
)
If @Total > 0 
BEGIN
	PRINT @FirstName + ' ' + @LastName + ' already exists in AgentClient table.'
END
 
 
 
SELECT @Total=COUNT(*) FROM AgentRole  
WHERE AgentID IN
(
SELECT AgentID FROM Agent 
WHERE FirstName=@FirstName
AND LastName=@LastName
)
If @Total > 0 
BEGIN
	PRINT @FirstName + ' ' + @LastName + ' already exists in AgentRole table.'
END
 
 
 
USE CCS_Molina_UAT
SELECT @Total=COUNT(*) FROM Agent 
WHERE FirstName=@FirstName  
AND LastName=@LastName
If @Total > 0 
BEGIN
	PRINT @FirstName + ' ' + @LastName + ' already exists in Molina.'
END
 
 
 
USE CCS_Molina_UAT
SELECT @Total=COUNT(*) FROM Agent 
WHERE FirstName=@FirstName
AND LastName=@LastName
If @Total > 0 
BEGIN
	PRINT @FirstName + ' ' + @LastName + ' already exists in Emblem.'
END
 
 
if @Total > 0 
BEGIN
	GoTo EndofScript
END
 
 
--Write Agent into appropriate databases.
BEGIN TRANSACTION
 
USE CCS_Central
INSERT INTO Agent (FirstName,LastName,Active)
VALUES(@FirstName,@LastName,1)
SET @NewAgentID=@@Identity
SET @Error=@@ERROR
 
INSERT INTO AgentRole (AgentID,RoleID)
VALUES(@NewAgentID,5)
SET @Error=@@ERROR
 
if @MolinaAccess=1 
Begin
	USE CCS_Central
	INSERT INTO AgentClient (AgentID,ClientID)
	VALUES(@NewAgentID,75)	
	SET @Error=@@ERROR
 
	USE CCS_Molina_UAT
	SET Identity_Insert Agent ON
	INSERT INTO Agent (AgentID,FirstName,LastName,Active)
	VALUES(@NewAgentID,@FirstName,@LastName,1)
	SET Identity_Insert Agent OFF
	SET @Error=@@ERROR
 
End
 
if @EmblemAccess=1
Begin
	USE CCS_Central
	INSERT INTO AgentClient (AgentID,ClientID)
	VALUES(@NewAgentID,2)
	SET @Error=@@ERROR	
 
	USE CCS_Emblem_UAT
	SET Identity_Insert Agent ON
	INSERT INTO Agent (AgentID,FirstName,LastName,Active)
	VALUES(@NewAgentID,@FirstName,@LastName,1)
	SET Identity_Insert Agent OFF
	SET @Error=@@ERROR
End
 
if @Error = -1
Begin
	ROLLBACK TRAN
End
else
begin
	COMMIT
end
 
EndofScript:

Answer : Deadly Script?

There are 2 BEGIN TRANSACTION statements. You need to commit both of them if you want to query any table modified by your script via a second query window.
Random Solutions  
 
programming4us programming4us