Gathering Space Information

  • I have written the following query, that loops through some servers and gathers space information. But I cannot get the dynamic openrowset part to work. I just get an error saying this is not a valid identifer. Can anyone help ?

    DECLARE @ServerName varchar(255)

    DECLARE @sql as varchar(6000)

    DECLARE c1 CURSOR READ_ONLY

    FOR

    SELECT sname

    FROM dbo.tbl_serversT

    OPEN c1

    FETCH NEXT FROM c1

    INTO @ServerName

    WHILE @@FETCH_STATUS = 0

    BEGIN

    Set @sql = 'SELECT a.* FROM OPENROWSET("SQLOLEDB","' + @servername + '";"NOTREALUSER";"NOTREALPASSWORD","SET FMTONLY OFF;EXEC master.dbo.xp_fixeddrives")AS a;';

    Exec @sql

    FETCH NEXT FROM c1

    INTO @ServerName

    END

    CLOSE c1

    DEALLOCATE c1

  • I just get an error saying this is not a valid identifer.

    Can you post the exact error message please?

    Edit:

    Apologies, the error is thrown because it's looking for each SELECT statement to be an sp or executable object; wrap your variable in brackets like so:

    EXEC (@sql)

    😀



    Ade

    A Freudian Slip is when you say one thing and mean your mother.
    For detail-enriched answers, ask detail-enriched questions...[/url]

  • Msg 203, Level 16, State 2, Line 20

    The name 'SELECT a.* FROM OPENROWSET("SQLOLEDB","NOTREALSERVER";"NOTREALUSER";"NOTREALPASSWORD","SET FMTONLY OFF;EXEC master.dbo.xp_fixeddrives")AS a;' is not a valid identifier.

  • I have changed the code to Exec (@sql) but I now get:

    Incorrect syntax near 'SQLOLEDB'.

  • Hi Jaaba

    Check the surface area configuration on your server, you have to enable to Open rowset option on the server, you will get this problem when this is not enabled.

    Cheers

    🙂

  • You may also need to change your quotes(") to apostrophes (')...



    Ade

    A Freudian Slip is when you say one thing and mean your mother.
    For detail-enriched answers, ask detail-enriched questions...[/url]

  • OpenRowset is configured on the server.

  • Changed all quotes(") to apostrophes (')

    still get:

    Incorrect syntax near 'SQLOLEDB'.

  • Jabba, can you use PRINT @sql to show a line of code before execution and post that here? I think you need to double up the apostrophes within the string.



    Ade

    A Freudian Slip is when you say one thing and mean your mother.
    For detail-enriched answers, ask detail-enriched questions...[/url]

  • If I change all the quotes (") to apostrophes (') it comes back with the SQLOLEDB ERROR when I change to Print @sql

    However if I run my original post with the quotes I get:

    SELECT a.* FROM OPENROWSET("SQLOLEDB","NOTREALSERVER";"NOTREALUSER";"NOTREALPASSWORD","SET FMTONLY OFF;EXEC master.dbo.xp_fixeddrives")AS a;

    as the results

  • Try copying the below and see if it works:

    Set @sql = 'SELECT a.* FROM OPENROWSET(''SQLOLEDB'',''' + @servername + ''';''NOTREALUSER'';''NOTREALPASSWORD'',''SET FMTONLY OFF;EXEC master.dbo.xp_fixeddrives'')AS a;';

    I've tried it using the linked servers on my machine (and changed login details!) and it worked okay.



    Ade

    A Freudian Slip is when you say one thing and mean your mother.
    For detail-enriched answers, ask detail-enriched questions...[/url]

  • I get this message:

    Could not find server 'SELECT a' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.#

    Ultimately I would like to avoid any solution that requires linked servers, as it would mean I would have to create thirty old linked servers.

  • The following code worked for me:

    DECLARE @ServerName varchar(255)

    DECLARE @sql as varchar(6000)

    DECLARE c1 CURSOR READ_ONLY

    FOR

    SELECT name

    FROM dbo.tbl_serversT

    OPEN c1

    FETCH NEXT FROM c1

    INTO @ServerName

    WHILE @@FETCH_STATUS = 0

    BEGIN

    Set @sql = 'SELECT a.* FROM OPENROWSET(''SQLOLEDB'',''' + @@servername + ''';''NOTREALUSER'';''NOTREALPASSWORD'',''SET FMTONLY OFF;EXEC master.dbo.xp_fixeddrives'')AS a;';

    Exec (@sql)

    FETCH NEXT FROM c1

    INTO @ServerName

    END

    CLOSE c1

    DEALLOCATE c1

    HTH

    MJ

  • That's brillant. Thank you all for your help.

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply