Is there a way to find a usage of a function in SQL server 2008?
Use in code:
SELECT * FROM sys.sql_modules WHERE definition LIKE '%MyFunc%'
UNION
SELECT * FROM sys.computed_columns WHERE definition LIKE '%MyFunc%'
UNION
SELECT * FROM sys.check_constraints WHERE definition LIKE '%MyFunc%'
UNION
SELECT * FROM sys.default_constraints WHERE definition LIKE '%MyFunc%'
I think I've covered all bases...
You can't use sys.comments because the type is nvarchar(4000)
The answer for SQL Server 2012:
SELECT DISTINCT sc.id,
so.name
FROM syscomments sc
INNER JOIN sysobjects so
ON so.id = sc.id
WHERE sc.TEXT LIKE '%functionname%'
ORDER BY 2
With SQL2012, use sp_depends in your database will list all usage inside.
ex.
EXEC sp_depends 'FunctionNameToSearch'
EXEC sp_depends 'TableNameToSearch'
EXEC sp_depends 'StoredProcNameToSearch'
Assuming that by "usage" you mean "usage statistics", then maybe SQL Server Profiler is useful for you.
Related
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
I have a stored procedure and a simple table.
I need to join this two object then allow user to see the result using Microsoft Query in Excel.
This is what I have. The Exec SP_Budget create global temp table and fill ##tmpBudget
exec SP_Budget;
Select g.name, g.address,g.Amount,b.BudgetAmt from gTable g
Left join ##tmpBudget b on b.NameID=g.NameID
Microsoft query can only do a simple
select * from tTable
how can I do this?
I have to have the stored procedure because it does UNpivot inside the SP_Budget
EDIT:
The more I think about this. I think I need to post the original SP_Budget
so here it is. Because maybe a better approach is to create a function rather than SP_Budget. Here is my SP_Budget. Is it possible to convert this SP to a function?
--this link show me how to build column list for the PIVOT/UNPIVOT http://stackoverflow.com/questions/9585094/sql-server-pivots-displaying-row-values-to-column-headers
--this link show me how to build column list of a specific table http://stackoverflow.com/questions/18775409/unpivot-with-dynamic-columns-plus-column-names
--here we build the dynamic query for the column list for the PIVOT/UNPIVOT
declare #sql AS NVARCHAR(MAX);
declare #cols nvarchar(max);
select #cols = coalesce(#cols+N',', N'') + quotename(c.name) from syscolumns c
inner join sysobjects o on c.id = o.id and o.xtype = 'u'
where o.name = 'AcctHist' and c.name not in ('PID', 'UID') and c.name like 'ptdbal%' and c.name <> 'PtdBal12' order by c.colid
--Construct the full T-SQL statement
--and execute dynamically
SET #sql = N'select
DB,Acct,Sub,cpnyid
,Fiscyr+
CASE WHEN len(Convert(varchar(2),CONVERT(int,right(Period,2))+1))=1
THEN ''0''+Convert(varchar(2),CONVERT(int,right(Period,2))+1)
ELSE Convert(varchar(2),CONVERT(int,right(Period,2))+1)
END "Period"
,Amounts
from (
SELECT A.DB,A.Sub,A.acct,A.FiscYr,A.CpnyID
, sum(A.PtdBal00) "PtdBal00"
,sum(A.PtdBal01) "PtdBal01"
,sum(A.PtdBal02) "PtdBal02"
,sum(A.PtdBal03) "PtdBal03"
,sum(A.PtdBal04) "PtdBal04"
,sum(A.PtdBal05) "PtdBal05"
,sum(A.PtdBal06) "PtdBal06"
,sum(A.PtdBal07) "PtdBal07"
,sum(A.PtdBal08) "PtdBal08"
,sum(A.PtdBal09) "PtdBal09"
,sum(A.PtdBal10) "PtdBal10"
,sum(A.PtdBal11) "PtdBal11"
FROM vz_Finance_Budget A
Group by A.DB,A.Sub,A.acct,A.FiscYr,A.CpnyID
)xx
UNPIVOT (Amounts for Period in ('+#cols+')) unpiv;';
EXEC sp_executesql #sql;
Edit2:
Thank you for all the suggestions. I decided that the limiting factor is microsoft query itself. Plus microsoft query should be replaced with something newer (I think). Instead of configuring my server to allow openrowset or OPENQuery. I will look into microsoft query replacement.
not sure if this is the right way to go. but I will update here. thanks.
Edit3: Trying this blog now: http://blogs.office.com/2010/06/07/running-a-sql-stored-procedure-from-excel-no-vba/ I will update when I complete it
As far as I know you can have explicit JOIN in Microsoft Query
Description of the usage of joins in Microsoft Query
SELECT ....
FROM tbl1, tbl2
WHERE tbl1.Id = tbl2.Id
Update
If you want to have the result set in Excel, You can use OPENROWSET to export the data from SQL Server to Excel.
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\testing.xls;',
'SELECT name, address, Amount, BudgetAmt FROM [Sheet1$]')
SELECT g.name, g.address, g.Amount, b.BudgetAmt FROM gTable g
LEFT JOIN #tmpBudget b ON b.NameID = g.NameID
As you just mentioned earlier, you need to save your proc result into a temp table first
sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
SELECT * INTO #tmpBudget
FROM OPENROWSET('SQLNCLI'
,'Server=(local)\SQL2008;Trusted_Connection=yes;'
,'EXEC SP_Budget')
I solved this using this blog below
http://blogs.office.com/2010/06/07/running-a-sql-stored-procedure-from-excel-no-vba/
basically in excel, when you click data tab - From other sources - instead of "from microsoft query". I select "from SQL server". With "from SQL Server". I have the option in the "Connection properties - definition" to select the command type to SQL (instead of table)
the blog explains this with screenshot... Thanks
I want to find list of stored procedures having dynamic queries in them.
Is there any way to that?
Here's a start. You can execute a dynamic SQL using EXEC and sp_executesql, so you want to search for stored procedures containing those commands:
SELECT
SP_NAME = o.name
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id = o.object_id
WHERE
m.definition LIKE '%EXEC%'
OR m.definition LIKE '%SP_EXECUTESQL%'
As suggested by Coder of Code, instead of sys.objects, you could JOIN it to sys.procedures.
Here are other method's according to my google search:
Using sys.procedures:
SELECT
SP_NAME = name
FROM sys.procedures
WHERE
OBJECT_DEFINITION(object_id) LIKE '%EXEC%'
OR OBJECT_DEFINITION(object_id)LIKE '%SP_EXECUTESQL%'
Using sys.sql_modules:
SELECT
SP_NAME = OBJECT_NAME(OBJECT_ID)
FROM sys.sql_modules
WHERE
OBJECTPROPERTY(OBJECT_ID, 'IsProcedure') = 1
AND (
definition LIKE '%EXEC%'
OR definition LIKE '%SP_EXECUTESQL%'
)
may be this will help
select * from sys.syscomments where text like '%exec%' AND 'SOME OTHER CONDITIONS'
Try this.
select b.name as Sp_Names from sys.syscomments a,sys.procedures b
where a.id=b.object_id and (a.text like '%exec%' or a.text like '%sp_executesql%')
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 to get the content (statements) of a stored procedure on databaseA and in serverA,
I am using SQL Server
use databaseA
go
sp_helptext 'YourProcedure'
go
Select Routine_name, Routine_Definition
From Information_Schema.Routines
Where Routine_Name = "YourStoredProcedureName"
use DATABASE_NAME
select [name], [text]
from sysobjects SO join syscomments SC
on SO.id = SC.id
where xtype = 'P'