Pull data from sys.columns in a different database I am running the following on one instance on the server:

SELECT c.*
FROM sys.columns c
WHERE c.object_id = OBJECT_ID(@tablename);


Everything works great.

I would like to run the same statement but point it to another database on the server.


  databaseName.dbo.tablename


I've tried the following (and other variations), but it does not work.

SELECT c.*
FROM sys.columns c
WHERE c.object_id = OBJECT_ID('databaseName.dbo.tablename');


Is there a way to get the column information on one database from another?

Code:

SELECT @SQL = @SQL + 'INSERT INTO myTable 
  (ColumnName,ColumnValue,SID,SName,RID,RName)
  VALUES (''' + QUOTENAME(c.name) + ''',NULL,0,NULL,0,NULL);'
            FROM sys.columns c
            WHERE c.object_id = OBJECT_ID(@tablename);

EXEC(@SQL);


Is there a way to do this dynamically?  If I had a @DBName parameter that I was passing in.
Add in the database you are looking to query from in front of the sys.columns.

SELECT c.*
FROM &lt;other database name&gt;.sys.columns c
WHERE c.object_id = OBJECT_ID(@tablename);