I have an ERP Database and it is big. One of the table gets updated by an SP, TRIGGER, FUNCTION or something else. Because, I watched the Profiler to find UPDATE or INSERT statements but I couldn't find ant UPDATE or INSERT. Therefore, the table should be updated by SP, TRIGGER, FUNCTION or something else.
Is there a helper to find in which SP,FUNCTION OR TRIGGERS the table is used? I want to give the table name and it will tell me where the table name is used?
In SSMS do the following
Server->Database->Tables-> tablename ->right click -> view
dependencies
select Object that depends on radio button to view the object's that were using your table
Export all script objects to a file and search the file. You can do this from SQL Server Management Studio. Right Click the database and go to Tasks > Generate Scripts.
In SSMS you can right click a table and then choose 'view dependencies' or use sp_depends.
Related
I just executed the following update statement on a view:
use [SERVER]
update [TABLE]
set USRN = 14201962
where COLUMN_UID = 4668083
Turns out there was a trigger that deleted that row entirely from the view instead of modifying that specific cell.
How can I get that row back? I'm assuming it might still be in the table that the row is associated with but I do not know what that table is. Is there a way for me to see which tables are part of the view so I can look through each one to try and locate the missing view row? Other suggestions are also welcome.
Thanks
You can right-click the view in SSMS and choose "Script As Create..." and see the query the view uses.
EDIT: This question was based on the incorrect premise that SQL VIEWS were cleared from a database when the user that created them disconnects from the server. Leaving this question in existence in case others have that assumption.
I'm trying to use views in my database, but I'm running up against an inability to save the code as a SQL Server object for repeated use.
I tried saving CREATE VIEW statements as procedures and user defined functions, but as many have answered on stack overflow, CREATE PROCEDURE and CREATE FUNCTION are incompatible with CREATE VIEW due to the only one in batch issue.
Obviously I don't want to retype my CREATE VIEW statements every time, and I'd prefer not to have to load them from text files. I must be missing something here.
You don't really "save" CREATE/ALTER statements. The create or alter statement changes the structure of the database. You can use SSMS to generate the statement again later by right clicking on the view, and choosing Script as->Create. This inspects the structure of the database and generates the statement.
The problem with this approach is your database now consists of both a structure definition(DDL) as well as its contents, the data. If you dropped/created the database to clear its data, you'd also have lost the structure. So you always need a database hanging around for the structure and back it up to ensure you don't ever lose the DDL.
Personally I would use Database Projects as part of Visual Studio and SQL Server Data Tools. This allows you to keep each View, Table, etc. as separate files, and then update the database using schema compare. The main benefit being you can separate the definition of the database from the database itself, and also source control or backup the DDL files.
If you really want to, you could create a view in a proc like this:
CREATE PROCEDURE uspCreateView AS
EXEC('CREATE VIEW... ')
Though, you'll have to escape single quotes in your view code with ''
However, I have to agree with the other comments that this seems like a strange thing to do.
Some other thoughts:
You can use sp_helptext to get the code of an existing view:
sp_helptext '<your view name here>'
Also, INFORMATION_SCHEMA.VIEWS includes a VIEW_DEFINITION column with the same code:
SELECT * FROM INFORMATION_SCHEMA.VIEWS
We can update a table in designer view in Pl/sql by using following query:
select * from table1 for update
and than by unlocking the designer view and do further changes.
Can we do the same in SQL Server? I tried to do the same in SQL Server than I got an error:
FOR UPDATE clause allowed only for DECLARE CURSOR
Guys any ideas??
In Management Studio, right-click the table name in Object Explorer, and choose "Edit Top 200 Rows". If this does not return the row(s) you want to edit, hit Ctrl+3 and this will allow you to modify the query that populates the grid. In all honesty, you should be learning proper DML (update, insert, delete) syntax instead of treating your table like a spreadsheet.
You won't be able to change the schema from the same designer, though; for that you'll have to use right-click > Design. (And again, proper DDL is better than relying on bug-laden and feature-restricted visual designers.)
I'm using sql server management studio 2008 to try and generate an alter script for each of my stored procedures in order to save the scripts for each revision. I can easily generate an alter script for each individual procedure, but I'm not trying to go through a hundred stored procedures manually.
I know that SSMS has an automated generate scripts function under task,
but the only options are create, drop and create, and drop.
I cant seem to figure out how to enable alter. I've already searched through many SO articles, as well as a little digging in msdn, and I've come up with nothing.
I'm hoping that the fine people of stackoverflow will be up to the challenge.
Thanks in advance
Use CHECK FOR OBJECT EXISTENCE option in Advanced Scripting Options.
Script will contain set of IF NOT EXISTS... CREATE commands and below ALTER for each stored procedure you wanted to script.
It's not a very elegant solution but it would definitely work. Why not generate create script and then just replace all occurrences of CREATE PROCEDURE with ALTER PROCEDURE.
You can generate stored procedures automatically from SQL SERVER Management Studio as following:
1) Right click on your database -> Tasks -> Generate Scripts
2) Select "specific database objects" then choose tables / stored procedures you want to generate script for them then press "Next"
3) In this window you can choose where you want to save your script, then you will find an option "Advanced", click it. Then you will find an option "Script DROP and Create",there are three options: Create, Drop, Drop and Create. Choose one as you want.
4) Then press ok, then "Next" and the script will be generated automatically.
If you want to change it from Create to Alter, just do "replace all" operation using any text editor.
Hope this answer helps others.
Well, Drop/Create is the same as alter. By stating that you would like to use alter then you are certain that the target objects exists. Why not just select the group from the object explorer and right click select DROP/Create. This should do the same.
Can I get the whole query which I used for creating a table, like we have sp_helptext to get the query of a stored procedure.
sp_helptext 'procedure_name'
Is there anything like this available for create table also in SQL server express?
I want to view the whole query which I wrote for creating a particular table and not the table structure.
Like if a deleted a table, and again want to create it, then I would have to type the whole query again, so I want a way through which I don't have to write the whole query again, like in mysql there is an option such as SHOW, which shows the table query?
In SQL Server Management Studio you can right-click on a table in the Object Explorer window and choose to generate the CREATE script into a new query window or put it in the clipboard or save it in a file.
Try sp_columns or sp_help. But this will not give you the CREATE TABLE text, you have to create this text for yourself.
You can also have a look at Catalog Stored Procedures