help with this script

  • Hi,

    i want to select all mdf files in a folder and check their property with

    dbcc checkprimaryfile to attach them,the problem is that this command dosen't work on locked(used mdf file).

    i need to remove the check on locked mdf files.

    is there a way to check if a file is used or locked through the SQL?

    mybe through xp_cmdshell?

    THX

  • Sounds like something you should be doing in PowerShell. Since you haven't actually shared a script it is a bit hard to comment on how you'd implement the check. A query like this would give you the file names of all databases that are offline, which means the files should not be locked:

    SELECT

    S.[name],

    S.filename

    FROM

    sys.databases AS D JOIN

    sys.sysaltfiles AS S

    ON D.database_id = S.dbid

    WHERE

    D.state_desc <> 'ONLINE'

    If you are looking for database files that have never been attached to the database instance then you could just get the list of ONLINE databases and skip those files.

Viewing 2 posts - 1 through 1 (of 1 total)

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