Printing Message On Inserted and Update On Tables
Printing Message On Inserted and Update On Tables
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
Result –
‘data inserted ‘
(1 row(s) affected)
Result------
'Can not remove emp'
‘Transaction has been canceled’
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
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
Examples
USE master
EXEC sp_help
USE pubs
EXEC sp_help publishers
ISDATE
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
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
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
--
ab
LEN
Returns the number of characters, rather than the number of bytes, of the
given string expression, excluding trailing blanks
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
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:
This example uses LTRIM to remove leading spaces from a character variable.
------------------------------------------------------------------------
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.
Result:-----------
2
(1 row(s) affected)
-----------4
(1 row(s) affected)
-----------
8
(1 row(s) affected)
-----------
16
(1 row(s) affected)