NEWID ( )DECLARE @myid uniqueidentifierExampleSET @myid =NEWID()PRINT 'Value of @myid is: '+ CONVERT(varchar(255), @myid)OBJECT_NAME(@@PROCID)PROCID: Returns the object identifier (ID) of the current Transact-SQL module. A Transact-SQL module can be a stored procedure, user-defined function, or trigger. @@PROCID cannot be specified in CLR modules or the in-process data access provider.Return Type : int@@TRANCOUNTThe BEGIN TRANSACTION statement increments @@TRANCOUNT by 1.ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION savepoint_name, which does not affect @@TRANCOUNT.COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT byRAISERRORItis used to returnmessages back to applications using the same format as a system error orwarning message generated by the SQL Server Database Engine.RAISERROR can return either:A user-defined error message that has been created using the sp_addmessage system stored procedure. These are messages with a message number greater than 50000 that can be viewed in the sys.messages catalog view.A message string specified in the RAISERROR statement.RAISERROR can also:Assign a specific error number, severity, and state.Request that the error be logged in the Database Engine error log and the Microsoft Windows application log.Substitute argument values into the message text, much like the C language printf_s function.Both RAISERROR and PRINT can be used to return informational or warning messages to an application. The message text returned by RAISERROR can be built using string substitution functionality similar to the printf_s function of the C standard library, whereas PRINT can only return a character string or character expression. A RAISERROR severity of 11 to 19 executed in the TRY block of a TRY…CATCH construct causes control to transfer to the associated CATCH block. Specify a severity of 10 or lower to return messages using RAISERROR without invoking a CATCH block. PRINT does not transfercontrol to a CATCH block.When RAISERROR is used with the msg_id of a user-defined message in sys.messages, msg_id is returned as the SQL Server error number, or native error code. When RAISERROR is used with a msg_str instead of a msg_id, the SQL Server error number and native error number returned is 50000.When you use RAISERROR to return a user-defined error message, use a different state number in each RAISERROR that references that error. This can help in diagnosing the errors when they are raised.Use RAISERROR to:Help in troubleshooting Transact-SQL code.Check the values of data.Return messages that contain variable text.Cause execution to jump from a TRY block to the associated CATCH block.Return error information from the CATCH block to the calling batch or application.ExampleRAISERROR ('your error message',16,2); --Here 16 is the Level and 2 is thestateDB_ID()Returns the database identification (ID) numberExample 1SELECT DB_ID() AS my_database_idExample 2SELECT DB_ID(N'AcharyaDatabase') AS my_database_idDB_NAME()Returns the Current database nameSELECT DB_NAME() AS my_db_Name;@@SERVERNAMEReturns the name of the local serverSELECT @@SERVERNAME AS 'Server Name'@@SERVICENAMEReturns the ServicenameSELECT @@SERVICENAME AS 'Service Name'USERSELECT USER AS 'User Name'@@SPIDSELECT @@SPID AS 'ID'SYSTEM_USERSELECT SYSTEM_USER AS 'Login Name'@@TEXTSIZESELECT @@TEXTSIZE AS 'Text Size'@@TIMETICKSReturns the number of microseconds per tick.SELECT @@TIMETICKS AS 'Time Ticks'@@TOTAL_READ,@@TOTAL_WRITEReturns the number of disk reads, not cache reads, by SQL Server since SQL Server was last startedSELECT @@TOTAL_READ AS 'Reads', @@TOTAL_WRITE AS 'Writes'@@VERSIONSELECT @@VERSION AS 'SQL Server Version'AVG | COUNT | MAX | MIN | STDEV | STDEVP | VAR | VARP | SUMSelect Sum(salary) from wagesSelect AVG(salary) from wagesSelect COUNT(salary) from wagesSelect MAX(salary) from wagesSelect MIN(salary) from wagesSelect STDEV(salary) from wagesSelect STDEVP(salary) from wagesSelect VAR(salary) from wagesSelect VARP(salary) from wages
Saturday, March 31, 2012
Useful SQL Functions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment