If it is a bug?
-
I have 4 databases named: db1, db2, db3, db4 on the same SQL Server. All 4 databases have the same table Department(ID, Name) but the data is different. For example:
Table ID Name
db1.Department 1 Dept1
db2.Department 1 Dept2
db3.Department 1 Dept3
db4.Department 1 Dept4I use SQL Query analyzer to execute the code fragment below:
declare @n int
declare @Name nvarchar(256)
set @n = 1
while (@n < 5)
begin
if (@n = 1) use db1
else if (@n = 2) use db2
else if (@n = 3) use db3
else if (@n = 4) use db4select \[Name\] from Department where \[ID\] = 1 set @Name = \[Name\] from dbo.Department where \[ID\] = 1 print @Name set @n = @n + 1
end
The results: --------------------------------- Dept1 Dept4 --------------------------------- Dept2 Dept4 --------------------------------- Dept3 Dept4 --------------------------------- Dept4 Dept4 But I think the correct result should be --------------------------------- Dept1 Dept1 --------------------------------- Dept2 Dept2 --------------------------------- Dept3 Dept3 --------------------------------- Dept4 Dept4 The question is: Is there anything wrong from me or from SQL???
-
I have 4 databases named: db1, db2, db3, db4 on the same SQL Server. All 4 databases have the same table Department(ID, Name) but the data is different. For example:
Table ID Name
db1.Department 1 Dept1
db2.Department 1 Dept2
db3.Department 1 Dept3
db4.Department 1 Dept4I use SQL Query analyzer to execute the code fragment below:
declare @n int
declare @Name nvarchar(256)
set @n = 1
while (@n < 5)
begin
if (@n = 1) use db1
else if (@n = 2) use db2
else if (@n = 3) use db3
else if (@n = 4) use db4select \[Name\] from Department where \[ID\] = 1 set @Name = \[Name\] from dbo.Department where \[ID\] = 1 print @Name set @n = @n + 1
end
The results: --------------------------------- Dept1 Dept4 --------------------------------- Dept2 Dept4 --------------------------------- Dept3 Dept4 --------------------------------- Dept4 Dept4 But I think the correct result should be --------------------------------- Dept1 Dept1 --------------------------------- Dept2 Dept2 --------------------------------- Dept3 Dept3 --------------------------------- Dept4 Dept4 The question is: Is there anything wrong from me or from SQL???
It sounds like your first conditional is falling through to the last case. Have you tried running it with conditions you know will always be false? It looks like it should work....
Have you answered an MTQ? Check out the stats!