Recommended SP/method to gain information [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
When I need more information about table and its column I always use the build-in stored procedure 'sp_help xxxxx' to retrieve more information.
What other method or SP are usable to use?

You can use sp_depends to get want tables and columns it is using
EXEC sp_depends yourProcedure;

I prefer to use dynamic management views (DMV) and functions (DMF) to get more information about database server..........The DMV/DMF's have been organized into the following different groups:
Common Language Runtime related
Database Mirroring related
Execution related
Full-Text Search related
Index related
I/O related
Query Notifications related
Replication related
Service Broker related
SQL Server Operation system
Transaction related

Just browsing to the table in SQL Server Management Studio will tell you quite a lot.
Table/Column definitions.
Indexes
Triggers
Constraints
Primary/Foreign Keys
Dependancies
etc, etc

Look into the sysobjects view (http://msdn.microsoft.com/en-us/library/ms177596.aspx):
SELECT * FROM sysobjects WHERE type = 'P'
Other sys view can be handy too.

How about using sp_columns
EXEC sp_columns #table_name = N'TableName'

Using sp_helptext will come in handy, it gives you the definition of a stored procedure, function or view.
I.E.:
CREATE PROC usp_MyProcedure AS SELECT * FROM TABLE
Runing the following will output the query above.
Exec sp_helptext 'usp_MyProcedure'

Related

SQL Server's SET NOCOUNT ON Postgresql equivalent [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've to help migrating a SQL Server 2008 database to a PostgreSQL 9.4.10 one. The original querys have SET NOCOUNT ON and SET NOCOUNT OFF several times, and I can't change this fact so ignoring the number of rows affected isn't a solution. That's why I need to find an equivalent to this statement, but all I can find are really old posts where they say I should ignore the result. Any ideas? Thanks.
EDIT: these SET NOCOUNTare inside a stored procedure that goes like this:
CREATE PROCEDURE [procedureName]
Declares
--Variables
WITH ENCRYPTION
AS
SET NOCOUNT ON
--Procedure's code
SET NOCOUNT OFF
GO
As I've said this code isn't mine, so I can't post any of it more than the source of my doubt.

how to rollback a transaction using rollback command? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have deleted a record from my table and after executing rollback command it shows "command completed successfully" but when i check it using select statement the table is still looking empty? why ?
Some database connection methods such as ODBC default to what's known as "autocommit" mode - that is, the database connection automatically issues a COMMIT after each statement is executed. JDBC does the same thing. I cannot say if this is what's happening in your case, but if so there's no way to do a ROLLBACK. Best of luck.
Rollback command takes you back to the latest committed state of the table.I guess your delete query might have contained some statement that committed the change(deletion of record).
Jason Clark,
I did a test using MySql and use the "begin", "delete" and "rollback", used the following SQL (an example):
begin; delete from aluno where id = 1; rollback;
In PostgreSQL, SQL syntax is the same and also worked.
Are you sure you used the correct SQL? I might have been some mistake? Is it really necessary to use "begin transaction" instead of just "begin"?
I hope this helps!

SQL procedure vs. script - terminology [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When one wants to update records in database using Data Manipulation Language, what would be right terminology to use:
UPDATE RECORDS IN TABLE VIA STORED PROCEDURE or
UPDATE RECORDS IN TABLE VIA SCRIPT (or UPDATE SCRIPT)
something else...
NOTE: I know that procedure is not same as script, I wrote question in rush. Real question is if you want to write to your DBA that the defect could be fixed by using (1) or (2) or (3) what would be the right choice. Sorry for not being precise.
If you want to DBA fix some defect, best approach is to write a script file with UPDATE statements, and save it for later use.
If you want that someone else (Job, DBA, App Code) frequently execute same code for updating records in table, then write stored procedure.
Good thing is that you can pass a parameter to stored procedure to affect on range of rows which will be updated.
ONE IMPORTANT THING: Stored procedure are optimized from SQL Optimizer and SQL creates most effective execution plan for it. When you execute it again, SQL find cached execution plan for that procedure and apply it. In this way, you achieve better performance when using stored procedure over script.
A "script" sounds to me like one or more statements that are sent to the database, to perform some action.
A "stored procedure" is that same bunch of statements, but already stored in the database so it can be activated with a simple command.
"Update via stored procedure" is not a synonim to "update via script", so why do you choosing either one or another as term?
Stored procedure - is an object (yes, technically it is some kind of scripit) created and stored in database.
Script - is just a script (sequence of statements). It can be stored in file or just created and executed "on the fly".
If you use just one update statement, then the most appropriate expression is "update records in table via update statement". If you use set of update statements, then it will be a script.
Procedure is an another DBMS object, usually called stored procedure. You can also define a procedure that update data in the database.
Non of the options fit me very well. Sure, if you know an update is being made from a Stored Procedure specifically, that would be fine to use. But an update can be done from many other ways and a Stored procedure can do many other things than just update.
I usually talk about queries and statements, for example:
Update records in a table via an update statement within a Stored Procedure
As for the use of script, I'm personally not that fond of it. There are already many more specific ways to talk about scripts, like Stored procedures, user defined functions, etc. That, for me, is a collection of statements and/or queries.

How to Schedule a trigger? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We had created a trigger for inserting a set of rows.
I want to schedule this trigger automatically and insert these data into another table. i.e, it has to execute automatically based on the period given.
Kindly suggest the possibilities steps for this.
Here i give you the clear explanation about the operation,
My project to transfer the data from MS SQL database table to MY SQL Database table. This event has to be scheduled. I have to select few fields from MS Sql table and that selected data to be transfer to My Sql table Automatically based on Scheduled.
You could use mysql event scheduler to configure and run your job.
Please have look into
http://dev.mysql.com/doc/refman/5.1/en/events-configuration.html
First, Trigger is one database object and trigger created over the any physical table. this is depends on table action so at every action of table trigger will execute.
we cant schedule trigger to execute on perticluar time.
There is alternative solution is: Enable and Disable trigger using scedule SQL Job.
Script is :
--To Enalble Trigger
ENABLE TRIGGER [Trigger_Name] ON [TableName]
GO
--To Disable Trigger
DISABLE TRIGGER [Trigger_Name] ON [TableName]
GO
If you're using MS SQL, then you can use SQL Server Agent to schedule a job/task to execute whatever you need done.
You can read more about it here http://msdn.microsoft.com/en-us/library/ms191439.aspx
Your best bet maybe to create a stored procedure and then in the scheduled job, simply execute that stored procedure to run.
Alternatively, if you don't have SQL Server Agent (requires full version of MS SQL not SQL Server Express) then you could possibly look into using Windows Scheduled Task to schedule a batch file to run periodically, making a call to the sqlcmd utility http://technet.microsoft.com/en-us/library/ms165702(v=sql.105).aspx which you should be able to setup to execute your stored procedure.
Hope this helps.

Case sensitive variable names in SQL Server? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When I execute this format of SQL command: SP_HELPTEXT Sproc1 .
The result set will display Could not find stored procedure 'SP_HELPTEXT'. But if i will replace the SQL command to lower case like sp_helptext Sproc1 , it definitely displays the content of Sproc1.
Im using the Sproc1 in my program and when the program executes Sproc1 it will return a message:
Must declare the variable '#Variable1'.
Although I've already declared that specific variable.
I have a hint that the issue is related to collation, case-sensitive or insensitive settings. Does anybody know how to resolve ?
Another situation where case sensitive variable names appear:
CREATE PROCEDURE Foo #customerID int AS
PRINT #customerId
You have a case sensitive server collation.
Your database has a (as you have shown) a case insensitive collation but when you have a case issue with variables it is the server collation that matters.
The same goes for sp_helptext which is a stored procedure defined in database master with lowercase. So when you call SP_HELPTEXT it is not found.
To fix your stored procedure to work in a case sensitive server collation you have to make sure that every reference to the variable #Variable1 is exactly that. Not #variable1 or #VARIABLE1.
Use this to check what server collation you have.
SELECT SERVERPROPERTY('collation');
From the SQL Server Books Online:
COLLATE (Transact-SQL)
The collation of an identifier depends on the level at which it is defined.
Identifiers of instance-level objects, such as logins and database names, are assigned the default collation of the instance.
Identifiers of objects within a database, such as tables, views, and column names, are assigned the default collation of the database.
For example, two tables with names different only in case may be created in a database with case-sensitive collation, but may not be created in a database with case-insensitive collation. For more information, see Database Identifiers.
The identifiers for variables, GOTO labels, temporary stored procedures, and temporary tables are in the default collation of the server instance.
Variables, GOTO labels, temporary stored procedures, and temporary tables can be created when the connection context is associated with one database, and then referenced when the context has been switched to another database.
See also
MSDN forums: Why are my SP's throwing a case error when pushing to a db using BIN collation?
Case sensitive variables in SQL Server
SQL Server stored procedure case sensitive?