You are on page 1of 4

Carino Drex Richard

February 20 2009

A. Built-In Functions

LOCATE
The LOCATE function returns the starting position of the
first occurrence of one string (called the search-string) within
another string (called the source-string). If the search-string
is not found and neither argument is null, the result is zero. If
the search-string is found, the result is a number from 1 to the
actual length of the source-string. If the optional start is
specified, it indicates the character position in the source-
string at which the search is to begin.
//example of LOCATE function
SELECT RECEIVED, SUBJECT, LOCATE('GOOD', NOTE_TEXT)
FROM IN_TRAY WHERE LOCATE('GOOD', NOTE_TEXT) <> 0

LCASE
Returns a string in which all the characters have been
converted to lowercase characters

//example of LCASE Function


Name_str = LCASE(Name_str)

LEFT
The LEFT function returns the leftmost integer characters of
expression.
// Example of LEFT function
SELECT LEFT(NAME, FIRSTNAME_LEN)
    FROM SYSIBM.SYSDUMMY1
Stored Procedure that uses user defined

B. Stored Procedure 1

CREATE PROCEDURE spDisplayNameInLowerCase


(
@cusName int
@cusId Varchar(10)
)
AS

Fx_LW(@cusName)

SELECT * FROM tblCus WHERE cusId=@cusId


GO

Stored Procedure 2

CREATE PROCEDURE spTest(

@val01 varchar(10) = ‘10′,


@val02 varchar(10) = NULL
)
Call:
dbo.ftest(@val01,@val02)
pTest(@val01,@val02)

go

Stored Procedure 3

CREATE PROCEDURE sp_DelLastLetter


(
@cusName int
@cusId Varchar(10)
@indx int
)
AS

@indx = Fx_GetLastLetterIndex(@cusName)
@cusname = Fx_RemoveLastLetter(@cusName,@indx)

SELECT * FROM tblCus WHERE cusId=@cusId


GO
Triggers that user Stored Procedure using the ExecCommand

Example 1

CREATE trigger tr_convertAgeToString


on tblCus
after Insert
as
set
nocount on
if exists
(select cusAge
from Inserted
where cusID >0)
Begin
Exec sp_Convert(tblCus.cusAge)
print 'Cannot Delete a non-zero quantity record'
Rollback
end

Example 2

CREATE TRIGGER TR_CPUMFR


ON [dbo].[CPUmfr]
AFTER INSERT, UPDATE, DELETE
AS

EXEC sp_createPulldownWriteFile 'cpumfr' , 'cpumfr'

CREATE PROCEDURE [dbo].[SP_CreatePulldownWriteFile]


(
@F as varchar(255), --Field
@T as varchar(255) --Table
)
AS

DECLARE @FileName varchar(50),


@bcpCommand varchar(2000)

Set @FileName = '"\\Tech-web\c$\' + @T + '.htm"'


SET @bcpCommand = 'bcp "EXEC InventoryManager.dbo.SP_CreatePulldown '
+ "'" + @F + "' , '" + @T + "'" + '"' +" queryout "
SET @bcpCommand = @bcpCommand + @FileName + ' -T -c'

EXEC master..xp_cmdshell @bcpCommand


GO

You might also like