View code inside procedure in SQL developer - sql

I have a procedure in SQL developer and I want to view what is written inside that procedure..how should I do it?
I am unable to see the code written inside the procedure but I am able to see the procedure structure when I run the query desc schema name.procedure name
Any help is appreciated..

You can use the connections tab which is in the left side of sql developer.
Click the + icon near schema name then + near procedure as shown in the pic.Under that you will have all the existing procedure. You can click any of it to view

SQL Developer
Browse to the connection name in the connections tab, expand the required object and just click, it will open the code in new tab.
ALL|USER|DBA_SOURCE view
Alternatively, you could query the DBA_SOURCE view:
SELECT text
FROM user_source
WHERE owner='<owner_name>'
AND name ='<procedure_name>'
AND type ='PROCEDURE';
make sure you put the owner_name and procedure_name in UPPER case, or in the exact case if you created using double-quotation marks.

Related

I created a SQL Server function. Now I can't find it in SSMS

I wrote a CREATE FUNCTION script, and created a function. But I can't find it in the LONG list of functions under the programability folder in SSMS. Where did SQL Server put my function. It's not in any of the folders, or if it is, I missed it. And I looked twice.
I did specify at the beginning of my script:
USE myDatabase
GO
before starting the
CREATE FUNCTION...
... in the script, so it shouldn't have gotten lost in the master database.
Look under \Programmability\Functions. To use those objects refer to this post SQLServer cannot find my user defined function function in stored procedure
I apologise. The questioner is an idiot.
All that I needed to do was right click on the Function folder in SSMS and choose "Refresh".
Sorry for wasting your time.
Check if your FUNC exists in that list
USE[MASTER]
select Name, Definition, type_desc FROM sys.sql_modules sm
INNER JOIN sys.objects so ON sm.object_id=so.object_id
where type_desc like '%func%'
In SSMS, try using the Object Explorer Details window (under the View menu) to list your scalar-valued (or whatever kind of) functions, where you can sort by Created in descending order.
I know this question has already been answered and all it needed was a refresh but I recently encountered the identical symptoms and the reason was because I was logged in as a user that did not have permission to see that function. Get the DB owner to grant the right permissions (execute permission) and you should be able to see it.

How do I find the tables and columns which make up a view

I have a view in one of our DBs and I need to find out where the values in the view come from
How do I go about this? (SQL Server Management Studio)
Try this
sp_helptext viewname
This will give you the view text and from there you can know the tables and it's columns.
SELECT TABLE_NAME, VIEW_DEFINITION FROM INFORMATION_SCHEMA.Views WHERE TABLE_NAME='view_Name'
In SQL Server Management Studio, go to the Object Explorer, find the view you're interested in, right-click on it, and pick
Script View As > CREATE To > New Query Editor Window
The SQL script that defines the view and thus also shows all the base tables it depends on will be scripted out into a new query editor window for you.
Or in Management Studio, you can also use the sp_depends system stored procedure to get all the dependencies for your object:
EXEC sp_depends #objname = N'HumanResources.vJobCandidate'
gives you an output of:

Setup Window application along with SQL Server database including structure, data, views, stored procedures, functions, relations

I am trying to setup (deploy) a Windows application along with a SQL Server database. I just followed this site (http://www.rajneeshverma.com/post/2012/08/16/Setup-and-Deployment-of-Windows-Application-with-SQL-Server-Database-using-Visual-Studio-2010.aspx). In this site the database tables, views and stored procedures generating script and keeping in a .txt file.
This one working great for table's structure and data separated with ; but when it comes to views, stored procedures it's not working. I tried it with same separator (;) for this text file but it getting error (Error 1001 : Incorrect syntax near the keyword 'CREAT')
Text file example:
CREATE VIEW [dbo].[Batch_Status_Report] AS ............... EMP ;
CREATE VIEW [dbo].[EMP_Report] AS ............... Employer ;
If I am not using any separator between the create view statements, then I get this error
Error 1001 : Incorrect syntax near the key word 'CREATE'. 'CREATE VIEW' must be the first statement in a query batch )
Text file example:
CREATE VIEW [dbo].[Batch_Status_Report] AS ............... EMP
CREATE VIEW [dbo].[EMP_Report] AS ............... Employer
Could you please any one can help me regarding this separator which one I need to use between views and stored procedures?
You could try passing the statement to create the view as a string to EXEC, and separate each EXEC statement with a semicolon, like so:
EXEC('CREATE VIEW [dbo].[Batch_Status_Report] AS ............... EMP');
EXEC('CREATE VIEW [dbo].[EMP_Report] AS ............... Employer');
This will also work for stored procedures etc.
However, a better approach would be to replace the semicolon separators with GO. GO can be used with sqlcmd to separate statements into batches. So, you can simply replace ';' with ' GO' and then use sqlcmd to run the single text file containing all your DDL statements.

Create Stored Procedure in Access 2007

Is it possible to create an Stored Procedure in Access 2007? If it is, how can i do it?
Thanks
You could save queries with parameters that works much like a SP.
You can created stored procedures in Access. I use Access 2007. I'm not sure of the capability in Access 2003. I've created stored procedures using the following process:
Create a query that performs the action you wish to take (Create(MakeTable), Read(Select), Update, Delete). Setup parameters if used. This query is for use as a template. You don't need to keep.
Switch query to SQL View and copy the query.
Use Cntl-G to switch over to Visual Basic. Use either the immediate window or create a subroutine using Insert | Procedure.
Paste your query and setup the following syntax to create the stored procedure.
CurrentProject.Connection.Execute "CREATE PROC sp_storedProcedureName (parm1 Text, parm2 Integer, whatever other parms & datatypes) AS sqlStatementFromQueryGoesHere;"
Run from immediate window or procedure. It should run clean if no errors.
Then go back to the Navagation pane and refresh (the stored proc won't show originally without refresh). Once refreshed, you have your stored proc showing in the queries category

Where does SQL Server store the stored procedure code?

I once needed the lines of the stored procedures to be able to trace whether I have a reference to some function, procedure or table, or sometimes to try to find something inside of the sp's code.
Where does SQL Server store the procedure's code?
Use sys.sql_modules because definition is nvarchar(max) because it will not truncate long code.
In INFORMATION_SCHEMA.ROUTINES the ROUTINE_DEFINITION column is only nvarchar(4000) so if you try view the text of a long procedure and you will see that it is truncated.
Use this to search for text in any procedure, view, function:
SELECT DISTINCT
o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition Like '%'+#Search+'%'
ORDER BY o.type_desc,o.name
use this to view the text of a given procedure, view, function:
select * from sys.sql_modules where object_id=object_id('YourProcedure')
You can use
select object_definition(object_id(routine_name)) from information_schema.routines
or
select object_definition(object_id) from sys.objects where name = 'foo'
or even
select object_definition(object_id('foo')); -- 'foo' is the table name
These versions are never truncated.
It stored it inside the system schema tables:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
See MSDN about the INFORMATION_SCHEMA.ROUTINES view:
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/c75561b2-c9a1-48a1-9afa-a5896b6454cf.htm
For a content search on this, you can do the follwing:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%search_string%'
If you are just trying to view the stored procedures code you go into the progammabiltity folder within your DB and they should be all stored in there under stored procedures.
If you are trying to search for references to other objects, then you can run a query like this:
SELECT *
FROM syscomments
WHERE TEXT LIKE '%searchstring%'
This will return any objects in the database that reference the search string. You can then look at these objects to see what stored procedures (and views and functions) are doing so.
View Dependencies
In SQL Server Management Studio, right-click on a table, and choose "View Dependencies". You will see every object that references the table
INFORMATION_SCHEMA
The actual code for a stored proc, view, constraint, etc is stored in SysComments. You should query this using the views provided in the schema Information_Schema. Here are all the components of the Information_Schema.
If you use SQL Server Management Studion, you can right click on the database you want, then click "Tasks -> Generate Scripts".
There you can generate a script with all the SP's in one single file, separated files, or directly to a query window, and search/change what you want.
Hope this helps.
(this is for SQL Server 2008, but i think 2005 has this functionality too)
EDIT:
You can also see one single SP code, by following this path "YourDB -> Programmability -> Stored Procedures", then right click on the SP you want to see, and click "Modify", and a query window is opened with the code.