Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. If it is a bug?

If it is a bug?

Scheduled Pinned Locked Moved Database
databasequestionsql-serversysadminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TPN
    wrote on last edited by
    #1

    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 Dept4

    I 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 db4

    select \[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???

    T 1 Reply Last reply
    0
    • T TPN

      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 Dept4

      I 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 db4

      select \[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???

      T Offline
      T Offline
      Terry ONolley
      wrote on last edited by
      #2

      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!


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups