Showing posts with label structure. Show all posts
Showing posts with label structure. Show all posts

Sunday, February 19, 2012

Accessing SWebmObjectSet Objects

I'm working in an environment where domain structure and firewall rules only allow access to a SQL Server - including OS - via a SQL Server client connection. I'm attempting to collect various pieces of information for an inventory database that are not normally accessible through standard t-sql calls.

A specific example is collecting network adapter and IP information via WMI and sp_OAxxx procedures. I've been able to interface WMI and retrieve the SWebmObjectSet collection with the information I want, but I can't seem to get to the individual objects in the collection for two reasons. First, t-sql doesn't have any sort of "for each" construct that allows me to iterate through the objects. Second, the Item() method of SWebmObjectSet requires an object path that I haven't been able to enumerate.

I'm avoiding enabling xp_cmdshell in SQL 2005 so calls such as "ipconfig /all" are not at option at this point.

How can I access the individual objects in the collection via t-sql? Is there another technology I might use? Remember - I can only access via a standard SQL client.

Here is some code I've come up with so far.

-- INITIALIZE SCRIPT

DECLARE@.wmiLocatorINT,

@.wmiServicesINT,

@.wmiObjectSetINT,

@.wmiObjectCountINT,

@.wmiObjectINT,

@.wmiNetAdapterNameNVARCHAR(200),

@.wmiNetAdapterIPNVARCHAR(200),

@.loopIdxINT,

@.oleSourceNVARCHAR(500),

@.oldDescNVARCHAR(500),

@.rcBIGINT,

@.msgNVARCHAR(400)

-- INITIALIZE WMI COM OBJECTS

EXEC @.rc = master.dbo.sp_OACreate 'WbemScripting.SWbemLocator', @.wmiLocator OUTPUT

IF @.rc <> 0 BEGIN

PRINT 'Create WMI object failed'

RETURN

END ELSE BEGIN

EXEC @.rc = master.dbo.sp_OAMethod @.wmiLocator, 'ConnectServer', @.wmiServices OUTPUT, '.'

IF @.rc <> 0 BEGIN

EXEC master.dbo.sp_OADestroy @.wmiLocator

RETURN

END

END

-- COLLECT DESIRED DATA

EXEC@.rc = master.dbo.sp_OAMethod @.wmiServices, 'InstancesOf', @.wmiObjectSet OUTPUT, 'Win32_NetworkAdapterConfiguration'

EXEC@.wmiObjectCount= master.dbo.sp_OAGetProperty @.wmiObjectSet, 'Count', @.wmiObjectCount OUTPUT

SELECT@.loopIdx= 0

WHILE @.loopIdx < @.wmiObjectCount - 1 BEGIN

EXEC@.rc= master.dbo.sp_OAMethod @.wmiObjectSet, 'Item', @.wmiObject OUTPUT, @.loopIdx

IF @.rc <> 0 BEGIN

EXEC@.rc= master.dbo.sp_OAGetErrorInfo @.wmiObjectSet, @.oleSource OUTPUT, @.oldDesc OUTPUT

END ELSE BEGIN

EXEC@.rc= master.dbo.sp_OAGetProperty @.wmiObject, 'Caption', @.wmiNetAdapterName OUTPUT

EXEC@.rc= master.dbo.sp_OAGetProperty @.wmiObject, 'IPAddress', @.wmiNetAdapterIP OUTPUT

END

SELECT@.loopIdx= @.loopIdx + 1

END

-- CLEANUP

EXEC master.dbo.sp_OADestroy @.wmiServices

EXEC master.dbo.sp_OADestroy @.wmiLocator

Is it fair to assume that you have completely explored and discarded the various system metadata, security, stastical and configuration functions, as well as the ODBC functions?

Would it be possible to have a Windows Scheduler task that would freqently run, using SQLCmd.exe to populate a table in the server with the desired information?

|||

If you mean the SQL Server metadata, security, statistical and configuration functions, then yes I have. But it is entirely possible I missed something which is why I posted the question.

It is possible to use the Windows Scheduler as you noted. I'm not a fan of installing DBA utilities on every server I manage if I can get away with a centralized solution. Additionally, there are many devices in scope that have firewall rules preventing console access and/or file transfer mechanisms. In short, the only mechanism I have is a standard SQL client. That is not to say that I couldn't petition the security team for relaxed access, but security is king here and it would be a battle I would likely lose.

One solution I've considered is use of the xp_cmdshell. Enabling it in SQL 2005 is possible, but I would prefer to leave it alone if I can.

Bruce.

|||

This really sounds like a task for an administrative WMI script -controlled and executed by the system administrators, retrieving the data and storing it in a central server for you to access. I would think that such an approach would molify the net administrator's security concerns. Easily done with MOM/SMS or whatever monitoring/management software is being used.

Thursday, February 9, 2012

Accessing Multiple databases from Stored procedure in SQL 2000

Hi ,

I have around 5 databases with same structure used to store data for different locations and services.

I have created an intermediate database which will have all the stored procedures to access data from above 5 databases.

My queries are common to all the databases. so,I would like to pass database name as an argument to my stored proc and execure query on specified database.

I tried this doing using "USE Databasename " Command. but it says ...We can not use "USE command " in stored proc or triggers. so ..what i did is ..

--

CREATE PROCEDURE TestDB_access(@.dbname varchar(100))
AS

BEGIN

DECLARE @.localDB varchar(100)

Set @.LocalDB=@.dbname

if @.LocalDB='XYZ'

EXEC ('USE XYZ ')
elseif @.LocalDB='ABC'

EXEC ('USE ABC')


Select * from Mytable

END

When I run this from my database , it gives me an error "unable to find table "mytable".

What is the best way to make my queries work with all the databases.

Thanks in advance

Venu Yankarla

Not sure what you are exactly trying to do but you can qualify a table with a database name and use it that way, however there is a limit in an SP of referring to 8 databases.

Try something like select * from Pubs..Authors where Pubs is the db name

|||

Thank you for your reply.

I can use the query "select * from Pubs..Authors where Pubs is the db name " if i know from which database i need to get the data.

In my case ... I want this dbname as a parameter to stored procedure. I can pass this to stored proc as parameter and bulid a string for SQL query and execute it. But I feel this as time consuming process as my queries will have atleast 5 joins in one query.

So , what I am looking for is .... Can we execute a query related to tables from a database (ABC or DEF or ETC) by using a stored proc in a database (XYZ)

Common thing in my case ..all databases has the same table structures but different data related to different locations.

in Query analyzer , we can execute queries on database (ABC or DEF) by staying in database (XYZ) using statement USE ABC and then execute SQL query.

USE ABC will change the database context and execute the query on "ABC" db tables.

Hope ..I am clear now..

Thanks

Venu yankarla

|||I think you will have to use sp_executesql for this