How do I find the query of a stored procedure? Is there any way to view it?
If you are using SQL Server Management Studio, under the database tree, choose Programmability | Stored Procedures. Just right click on the procedure name and click modify.
But the database role will also affect your action.
There are many ways to do this one simple way is
sp_helptext 'Your_SP_Name'
or
SELECT OBJECT_DEFINITION(OBJECT_ID('Your_SP_Name'))
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%%'
AND OBJECTPROPERTY(id, 'IsProcedure') = 1
GROUP BY OBJECT_NAME(id)
Try This Its Worked...
Related
How to find the database name for a particular table when we have created a table and we forgot the database where we have created the same.
I have found the solution, we can simply find a database where we have created the table.
SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'tablename'
Try this EXEC sp_MSforeachdb
EXEC sp_MSforeachdb 'USE ? SELECT sc.TABLE_CATALOG, sc.TABLE_SCHEMA, sc.TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS sc WHERE TABLE_NAME=''YourTableName'''
if you are using SQL Server 2012 or more then simply you can find DataBase name using INFORMATION_SCHEMA like
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME ='your_table_name'
Here TABLE_CATALOG column shows DataBase Name.
There is no SQL Server ready solution to your problem.
To resolve it you need to loop through each database and to put the information schema into new table and then to query it.
Some idea can be found in the site bellow:
https://blog.sqlauthority.com/2008/04/29/sql-server-find-table-in-every-database-of-sql-server/
I have a table in a database. The fields of the table are being updating by some stored procedure from some other table. Is there any possibility that I can find the stored procedures which are acting on a particular table???
If you want to know which stored procedures are likely to touch your table, you could search that in sys.procedures something on the lines of:
select * from sys.procedures
where object_definition(object_id) like '%tablename%'
-- or probe information_schema
select * from information_schema.routines
where routine_definition like '%tablename%'
Object_Definition
Returns the Transact-SQL source text of the definition of a specified object.
https://msdn.microsoft.com/en-us/library/ms176090(v=sql.105).aspx
When we look into sys.procedures, there'll be a field called object_id. Each object in SQL Server has an ID. object_definition will retrieve definition of that object - in our case, text of the procedure will be retrieved.
Check out the sys.dm_sql_referencing_entities view.
But this won't work if the SP build dynamic SQL to access the table (not schema bound). You might need to use trace or SQL Profiler to help out.
Maybe you use this query. You got a list store procedure in actual database
SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE= 'PROCEDURE'
I created many numbered stored procedures in SQL Server 2008.
Their names are e.g.: dbo.ProcName, dbo.ProcName;2, dbo.ProcName;3,...
When I use rightclick on procedure name in procedures list in Management Studio 2008 and then click to Modify I can see and edit all stored procedure as I wrote above.
But in Management Studio 2012, after same action, I can see only the first procedure.
How to see and edit all stored procedures with the same name and suffix with semicolon?
Sounds like you are talking about numbered stored procedures.
I can reproduce the same behaviour. After running the following
CREATE PROCEDURE [dbo].[foo];1 #x int AS
PRINT 'x is ' + CONVERT(varchar(8), #x)
GO
CREATE PROCEDURE [dbo].[foo];2 #x char AS
PRINT 'x is ' + #x
GO
Selecting "Foo" in object explorer then "Modify" shows both versions in SSMS 2008 but only the first one in SSMS 2012.
These have been deprecated since 2005 so I wouldn't be surprised if it is a deliberate decision not to support them in the tools any more. Looks like Drop and Create still lists both versions though.
Another workaround:
EXEC sp_helptext 'dbo.ProcName';
You'll have to inject the GOs yourself, though.
You can also get the metadata this way:
SELECT definition
FROM sys.sql_modules
WHERE [object_id] = OBJECT_ID('dbo.ProcName')
UNION ALL
SELECT definition
FROM sys.numbered_procedures
WHERE [object_id] = OBJECT_ID('dbo.ProcName');
And this way:
SELECT [text] FROM syscomments
WHERE id = OBJECT_ID('dbo.ProcName');
And you can go through the Generate Scripts wizard:
right-click your database and choose Tasks > Generate Scripts
On the 'Choose Objects' screen, select 'Select specific database objects, check 'Stored Procedures', and expand to select the root name of the numbered stored procedure(s) you want to script.
On the 'Set Scripting Options' screen, choose 'Save to new query window.'
Click Next > Next > Finish.
I also filed a bug on this:
http://connect.microsoft.com/SQLServer/feedback/details/764197/ssms-2012-inconsistently-handles-numbered-procedures
I somehow managed to create a table in a database with a null table schema.
I can't query the table since it has no owner, and altering the table doesn't work for the same reason.
I would alter the table using:
ALTER SCHEMA null TRANSFER dbo.SubscriptionAnswerMR
But that doesn't work.
The information_schema.tables looks like this:
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
q_Profiles NULL SubscriptionAnswerMR BASE TABLE
So my question is: How do I change q_Profiles' table_schema?
SQL Server 2000 (edit)
Microsoft SQL Server Management Studio 2008R2
You should be able to verify that your table is fine by seeing the result of the following query:
SELECT u.name
FROM q_Profiles..sysobjects AS o
INNER JOIN q_Profiles..sysusers AS u
ON o.uid = u.uid
WHERE o.name = 'SubscriptionAnswerMR';
This should be dbo unless someone explicitly created them with a different owner or used sp_changeobjectowner. Which you can use if you find that sysobjects also has the wrong answer:
EXEC sp_changeobjectowner 'SubscriptionAnswerMR', 'dbo';
ALTER SCHEMA is not valid here because it was introduced in SQL Server 2005. Though it would be useful for you to describe what "doesn't work" means.
INFORMATION_SCHEMA is a horribly unreliable set of views as #Pondlife points out. Also see the following, which doesn't help you much in SQL Server 2000, but should help going forward:
The case against INFORMATION_SCHEMA views
Also as a side note you seem to be confused about tables and database. TABLE_CATALOG is the database, not the table.
Did you note this comment in the documentation?
Do not use INFORMATION_SCHEMA views to determine the schema of an
object. The only reliable way to find the schema of a object is to
query the sys.objects catalog view or use the OBJECT_SCHEMA_NAME
function.
How do you add a new schema to a database? I am creating a new table and would like to select my own schema from the properties list, but I don't know how to create it. I am using SQL Server Management 2008.
Use the CREATE SCHEMA syntax or, in SSMS, drill down through Databases -> YourDatabaseName -> Security -> Schemas. Right-click on the Schemas folder and select "New Schema..."
Here's a trick to easily check if the schema already exists, and then create it, in it's own batch, to avoid the error message of trying to create a schema when it's not the only command in a batch.
IF NOT EXISTS (SELECT schema_name
FROM information_schema.schemata
WHERE schema_name = 'newSchemaName' )
BEGIN
EXEC sp_executesql N'CREATE SCHEMA NewSchemaName;';
END
I use something like this:
if schema_id('newSchema') is null
exec('create schema newSchema');
The advantage is if you have this code in a long sql-script you can always execute it with the other code, and its short.
Best way to add schema to your existing table: Right click on the specific table-->Design -->
Under the management studio Right sight see the Properties window and select the schema and click it, see the drop down list and select your schema. After the change the schema save it. Then will see it will chage your schema.
You can try this:
use database
go
declare #temp as int
select #temp = count(1) from sys.schemas where name = 'newSchema'
if #temp = 0
begin
exec ('create SCHEMA temporal')
print 'The schema newSchema was created in database'
end
else
print 'The schema newSchema already exists in database'
go
In SQL Server 2016 SSMS expand 'DATABASNAME' > expand 'SECURITY' > expand 'SCHEMA' ; right click 'SCHEMAS' from the popup left click 'NEW SCHEMAS...' add the name on the window that opens and add an owner i.e dbo click 'OK' button