Printing message on inserted and update on tables
USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'reminderx' AND type = 'TR')
DROP TRIGGER reminderx
GO
CREATE TRIGGER reminderx
ON emp
FOR INSERT, UPDATE
AS print 'data inserted '
GO
select * from emp
insert into emp values('12','dev')
Result –
‘data inserted ‘
(1 row(s) affected)
Roll back Transaction on deleting data
CREATE TRIGGER del ON [emp]
FOR DELETE
AS
IF (@@ROWCOUNT >1)
BEGIN
PRINT 'Can not remove emp'
PRINT 'Transaction has been canceled'
ROlLBACK
END
delete from emp
select * from emp
Result------
'Can not remove emp'
‘Transaction has been canceled’
• @@ROWCOUNT—will return no of records affected
• @@ERROR---Returns the error number for the last Transact-SQL
statement executed.
• SET ROWCOUNT---
Causes Microsoft® SQL Server™ to stop processing the query after the specified
number of rows are returned.
SET ROWCOUNT 20
GO
delete from emp
GO
Set rowcount 20—will set no of effected rows to 20 .
Suppose emp contains 100 records then it will delete only 20 records
Select * from emp will also display only 20 records .
Insert query will inset only one record at time
@@SPID--Returns the server process identifier (ID) of the current user process
ABS--Returns the absolute, positive value of the given numeric expression.
SELECT ABS(-1.0), ABS(0.0), ABS(1.0)
Here is the result set:
---- ---- ----
1.0 .0 1.0
Begin –End
USE pubs
GO
CREATE TRIGGER deltitle
ON titles
FOR delete
AS
IF (SELECT COUNT(*) FROM deleted, sales
WHERE sales.title_id = deleted.title_id) > 0
BEGIN
ROLLBACK TRANSACTION
PRINT 'You can't delete a title with sales.'
END
Alter Trigger
USE pubs
GO
CREATE TRIGGER royalty_reminder
ON roysched
WITH ENCRYPTION
FOR INSERT, UPDATE
AS RAISERROR (50009, 16, 10)
GO
-- Now, alter the trigger.
USE pubs
GO
ALTER TRIGGER royalty_reminder
ON roysched
FOR INSERT
AS RAISERROR (50009, 16, 10)
sp_help
Reports information about a database object (any object listed in the sysobjects
table), a user-defined data type, or a data type supplied by Microsoft® SQL Server
The sp_help procedure looks for an object in the current database only.
When name is not specified, sp_help lists object names, owners, and object types for
all objects in the current database. sp_helptrigger provides information about
triggers.
Permissions
Execute permissions default to the public role.
Examples
A. Return information about all objects
This example lists information about each object in sysobjects.
USE master
EXEC sp_help
B. Return information about a single object
This example displays information about the publishers table.
USE pubs
EXEC sp_help publishers
ISDATE
Determines whether an input expression is a valid date.
This example checks the @datestring local variable for valid date data.
DECLARE @datestring varchar(8)
SET @datestring = '12/21/98'
SELECT ISDATE(@datestring)
Here is the result set:
-----------
1
IS [NOT] NULL
Determines whether or not a given expression is NULL.
This example returns the title number and the advance amount for all books in
which either the advance amount is less than $5,000 or the advance is
unknown (or NULL). Note that the results shown are those returned after
Example C has been executed.
USE pubs
SELECT title_id, advance
FROM titles
WHERE advance < $5000 OR advance IS NULL
ORDER BY title_id
ISNUMERIC
Determines whether an expression is a valid numeric type.
ISNUMERIC returns 1 when the input expression evaluates to a valid integer,
floating point number, money or decimal type; otherwise it returns 0. A
return value of 1 guarantees that expression can be converted to one of these
numeric types.
LEFT
Returns the left part of a character string with the specified number of
characters.
This example returns the five leftmost characters of each book title.
USE pubs
GO
SELECT LEFT(title, 5)
FROM titles
ORDER BY title_id
GO
Here is the result set:
-----
The B
Cooki
You C
The G
SELECT LEFT('abcdefg',2)
GO
Here is the result set:
--
ab
LEN
Returns the number of characters, rather than the number of bytes, of the
given string expression, excluding trailing blanks
This example selects the number of characters and the data in
CompanyName for companies located in Finland.
USE Northwind
GO
SELECT LEN(CompanyName) AS 'Length', CompanyName
FROM Customers
WHERE Country = 'Finland'
Here is the result set:
Length CompanyName
----------- ------------------------------
14 Wartian Herkku
11 Wilman Kala
LOWER
Returns a character expression after converting uppercase character data to
lowercase.
This example uses the LOWER function, the UPPER function, and nests the
UPPER function inside the LOWER function in selecting book titles that have
prices between $11 and $20.
USE pubs
GO
SELECT LOWER(SUBSTRING(title, 1, 20)) AS Lower,
UPPER(SUBSTRING(title, 1, 20)) AS Upper,
LOWER(UPPER(SUBSTRING(title, 1, 20))) As LowerUpper
FROM titles
WHERE price between 11.00 and 20.00
GO
Here is the result set:
Lower Upper LowerUpper
-------------------- -------------------- --------------------
the busy executive's THE BUSY EXECUTIVE'S the busy executive's
cooking with compute COOKING WITH COMPUTE cooking with compute
straight talk about STRAIGHT TALK ABOUT straight talk about
LTRIM
Returns a character expression after removing leading blanks.
This example uses LTRIM to remove leading spaces from a character variable.
DECLARE @string_to_trim varchar(60)
SET @string_to_trim = ' Five spaces are at the beginning of this
string.'
SELECT 'Here is the string without the leading spaces: ' +
LTRIM(@string_to_trim)
GO
Here is the result set:
------------------------------------------------------------------------
Here is the string without the leading spaces: Five spaces are at the
beginning of this string.
POWER And WHILE
Returns the value of the given expression to the specified power.
This example returns POWER results for 21 to 24.
DECLARE @value int, @counter int
SET @value = 2
SET @counter = 1
WHILE @counter < 5
BEGIN
SELECT POWER(@value, @counter)
SET NOCOUNT ON
SET @counter = @counter + 1
SET NOCOUNT OFF
END
GO
Result:-----------
2
(1 row(s) affected)
-----------4
(1 row(s) affected)
-----------
8
(1 row(s) affected)
-----------
16
(1 row(s) affected)