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 2000 Forums
 SQL Server Development (2000)
 xp_cmdshell behave differently

Author  Topic 

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-21 : 19:10:08
Hi -,

I noticed that when I was using xp_cmdshell to retrieve data for servers and databases users from table [sys.database_principal] it behave differently (gave me different answers) running on local SQL server instance and other SQL server instances....

Could anyone please let me know why is that and how do I fix the problem please?

Thanks a lot!

Connie

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-21 : 19:11:24
You'll need to provide more detail.

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

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-21 : 20:07:09
because the table is different on different tables?

post some code

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-12-22 : 17:11:38
are you saying the instances are identical? If not....

There is nothing wrong with xp_cmdshell



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 17:15:51
Hi Brett,

Sorry I don't understand what you meant by those instances are identical.... do you mean they are of the same version of SQL servers?

Thanks!

Connie
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-22 : 17:27:46
Why are you expecting them to be the same? They are different servers...

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

Subscribe to my blog
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 17:47:59
Hi Tara,

they are different serves but I am accessing the same table: the master.sys.database_principals on serverA.

Connie :)
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 17:49:01
I meant I was just querying the same table from different servers..... Could that lead to different running results?
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 18:15:23
Hi there,

It is querying the same table. I logged in SQL server using windows login, and connected to a local instance (serverA) as well as another server instance (serverB); however, when I was running query:

SET NOCOUNT ON SELECT * FROM master.sys.database_principals WHERE type IN ('U')

on the local instance it gave me correct answer; but if I run query:

exec xp_cmdshell 'sqlcmd -S "ServerA" -Q "SET NOCOUNT ON SELECT * FROM master.sys.database_principals WHERE type IN (''U'')" -s "|" -W'

from the other instance (ServerB) I was connected to, it gave me a different answer.

Connie
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-22 : 20:01:43
Are you sure it's not just sorting it differently? Could you show us the output of both?

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

Subscribe to my blog
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 21:31:11
Hi Tara,

I am sure it is not just sorting it differently cause the time when i was running it on serverB gave me 5 rows while the time when I was running it on a local instance it gave me only 4 rows.

Connie
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 21:37:13
Also, if I was to query for SQL logins ( where type in 'S') it gave me different results too.
the running results running on local instance is: dbo, guest, INFORMATION_SCHEMA and sys; but if I run it on instance serverB it gave me one more result: dwuser.

I noticed something: if on instance serverB i run below query:

select distinct (d.name)
from
UsersDatabaseCurrent d, UsersServerCurrent s
where
d.name = s.name
and
d.DatabaseName = 'master'

it gave me the extra result.

I was just wondering why this is happening?

Thanks a lot!

Connie
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 21:40:57
i was using SQl logins as an example cause it gives me less results and it was more convenient to type.

tables [UserDatabaseCurrent] and [UsersServerCurrent] are contains all the users for databases on all server and all servers; they are populated by extracting info from sys.database_principals and sys.server_principals
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-22 : 23:31:21
you mean you are executing this query

SELECT * FROM master.sys.database_principals WHERE type IN ('U')


and different SQL Server and you expect the same result to return ?




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-22 : 23:54:29
Hi KH,

yes I am running the query above from two different servers (serverA and serverB) but querying the same table (serverA.master.sys.database_principals).
the query i ran on serverA (local instance) is as below:

SET NOCOUNT ON SELECT * FROM master.sys.database_principals WHERE type IN ('U')


the query I ran on serverB (the other instance) to query the master database users for serverA is as below:

exec xp_cmdshell 'sqlcmd -S "ServerA" -Q "SET NOCOUNT ON SELECT * FROM master.sys.database_principals WHERE type IN (''U'')" -s "|" -W'

Since they are querying the same table, the results returning should be the same right?

Connie


Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-23 : 01:32:18
quote:
Since they are querying the same table, the results returning should be the same right?

No. same table but different Server. Unless you have created the same users in both of the servers


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-12-23 : 01:35:29
I think what Connie is saying is that it's the same table, just one is querying it locally and the other is querying it remotely. So both are querying serverA, and they aren't returning the same result set.

My only guess is that you aren't actually querying the same server even though you think you are. Have you checked @@SERVERNAME?

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

Subscribe to my blog
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-25 : 05:08:18
Hi Tara,

yeah that was what I meant. I was querying the same system view master.sys.database for the same server (serverA) both locally and remotely;but i got different results.

Could you please have a look at the query I used:

locally: SET NOCOUNT ON SELECT * FROM master.sys.database_principals WHERE type IN ('U')

remotely: exec xp_cmdshell 'sqlcmd -S "ServerA" -Q "SET NOCOUNT ON SELECT * FROM master.sys.database_principals WHERE type IN (''U'')" -s "|" -W'

if I am not actually querying the same server, what was wrong with my query?

Thanks a lot!!!!

Connie


Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-25 : 05:21:00
I have noticed though, when I was querying other tables or views both locally and remotely querying gave me correct answers, but when it came to querying about logins (such as: syslogins etc.) the running results are different locally and remotely...

I was so confused....
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2011-12-25 : 09:17:32
When you run xp_cmdshell, it runs in the context of the service account. When you run then use SQLCMD without specifying the login credentials it will use Windows Authentication of the service account from ServerB.

In this case, that login only has PUBLIC access and therefore can only see the public database principals avaialble in the master database.

Specify a different user login for SQLCMD - or grant the service account higher rights on SystemB to see additional database principals.

Jeff
Go to Top of Page

ConnieXujuan
Starting Member

48 Posts

Posted - 2011-12-27 : 17:23:57
Thanks Jeff! I think that is the problem. I have used an account that has higher permission and had a look.... it is now correct!

Connie :)
Go to Top of Page
    Next Page

- Advertisement -