How can we find which table is locked in the database? Please, suggest.
You can use sp_lock (and sp_lock2), but in SQL Server 2005 onwards this is being deprecated in favour of querying sys.dm_tran_locks:
select
object_name(p.object_id) as TableName,
resource_type, resource_description
from
sys.dm_tran_locks l
join sys.partitions p on l.resource_associated_entity_id = p.hobt_id
sp_lock
When reading sp_lock information, use the OBJECT_NAME( ) function to get the name of a table from its ID number, for example:
SELECT object_name(16003073)
EDIT :
There is another proc provided by microsoft which reports objects without the ID translation : http://support.microsoft.com/kb/q255596/
Related
The below query is used for getting last execution time and date but can I get the parameter used in it while execution in SQL Server?
SELECT
o.name,
s.last_execution_time,
s.type_desc,
s.execution_count
FROM
sys.dm_exec_procedure_stats s
INNER JOIN
sys.objects o ON s.object_id = o.object_id
WHERE
DB_NAME(s.database_ID) = 'XYZ' --Database Name
AND o.name LIKE ('%ABC%') --Object Name
Thanks for #HoneyBadger's comment, and this will help for you:
The article shows how to find compiled parameter values for SQL Server cached plans.
I am connecting to Microsoft SQL Server using Oracle SQL Developer.
Describe table-name is not giving me any results. Can anyone please help me out with the right command to use to view SQL Server table structure in Oracle SQL developer?
Try this:
-- custom table information
select schema_name(t.schema_id)+'.'+t.name as TableName,
t.*
from sys.tables t
where t.name = 'MyTableName'
-- table columns information
select schema_name(t.schema_id)+'.'+t.name as TableName,
TYPE_NAME(t2.system_type_id) as DataType,
t2.*
from sys.tables t
inner join sys.columns t2 on t2.object_id = t.object_id
where t.name = 'MyTableName'
order by 1,
t2.column_id
Or this:
-- custom table information
exec sp_help 'MyTableName'
-- table columns information
exec sp_columns 'MyTableName'
DESC is an Oracle client command - works in SQLPlus, SQLcl, and SQL Developer - WHEN connected to an Oracle Database.
The best we have to offer you is, open the table from your browser, and see the Columns page.
Or like someone has offered, write query or use the provided SP that MSFT gives you.
Thank you guys for your answers. I have found one more method which is below
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='XX_TABLE_NAME';
Thought of sharing as it might be helpful for others
Shift + F4
or Right click on tablename and click on popup Describe
We would like to remove developer comments from stored procedures for some specific reason.
Is there any mechanism available within SQL Server 2008 R2?
It may be possible to script alter statements based on meta data and strip the comments in the process.
As a starting point take a look at sys.sql_modules
SELECT OBJECT_SCHEMA_NAME(m.object_id) AS [SchemaName],
OBJECT_NAME(m.object_id) AS [ObjectName], o.type, m.definition
FROM sys.sql_modules m
inner join sys.all_objects o on o.object_id = m.object_id
where o.is_ms_shipped = 0
order by OBJECT_NAME(m.object_id) ;
But I don't know why you don't just manually script the sp's to a text file and strip it before running.
I am using Microsoft SQL Server 2008. I have a stored procedure. Is there a simple query I can execute that will give me the parameter names?
I have found this Link but it is not for Microsoft SQL Server 2008.
To get names only you can use this query:
SELECT name
FROM sys.parameters
WHERE object_id = OBJECT_ID('YourProcedureName')
To get more detailed info (name, type and length of parameter):
SELECT p.name AS ParameterName, t.name AS ParameterType, p.max_length AS ParameterLength
FROM sys.parameters AS p
JOIN sys.types AS t ON t.user_type_id = p.user_type_id
WHERE object_id = OBJECT_ID('YourProcedureName')
On top of what Marek stated, you can also retrieve them programatically using the DeriveParameters method in the .NET library: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.deriveparameters.aspx
Check out my blog on database files and objects. http://craftydba.com/?p=2901
I have a stored procedure called SP_STORE_PRIMES in my sample [MATH] database.
One way is to use the sys.parameters table. This can be optionally joined to types. Below is joined to sys.objects.
-- Parameters to SP & FN
select o.name, p.* from sys.parameters p join sys.objects o
on p.object_id = o.object_id where is_ms_shipped = 0
go
A older system stored procedure is sp_sproc_columns.
-- Older system stored proc - show all parameters to one
sp_sproc_columns #procedure_name = 'SP_STORE_PRIMES'
go
Both ways will get you where you want to go.
I need the behaviour of SQL Server 2005 where function OBJECT_NAME takes two arguments, obj id and db id, while SQL Server 2000 takes only obj id so the execution must be in the context of the database to which inspected object belongs to.
Solution must be possible to implement in a function, so it can be used in a select query.
In SQL 2005 and up it is of course trivial to do this. The problem is SQL 2000. I used 2000 a lot back when, but no longer have access to any installations of it; the rest of this is largely from memory, and may be inaccurate.
The key thing is how to retrieve data from a database other than the "current" database, when you cannot know what that other database (or databases) will be at the time the code is written. (Yes, the db_id parameter is very convenient!) For this problem and for similar problems, the general work-around is to create dynamic code, something like:
SET #Command = 'select name from ' + #dbname + '.dbo.sysobjects where object_id = ' + #ObjectId
EXECUTE (#Command)
The problem is, I'm pretty sure you can't run dynamic code within functions (or perhaps just within SQL 2000 functions).
You might have to resort to creating a temp table, populating it via dynamic query, and then using it within the "main" query you are trying to write. Psuedo code would be like:
CREATE #TempTable
IF SQL2000 or earlier
INSERT #TempTable EXECUTE (select data from TargetDb.dbo.sysobjects)
-- Note that the entire insert may need to be in the dynamic statement
ELSE
INSERT #TempTable SELECT [from query based on object_id]
SELECT [the data you need]
from YourTable
join #TempTable
In SQL 2008 and up, use:
OBJECT_NAME ( object_id [, database_id ] )
for example:
SELECT TOP 10
object_schema_name(objectid, dbid) as [SchemaName],
object_name(objectid, dbid) as [ObjectName],
e.*
from sys.dm_exec_cached_plans P
CROSS APPLY sys.dm_exec_query_plan(P.plan_handle) E