search:
Navigation
Home
About
Library
Contact
Snippet Library
ColdFusion
338
ASP
201
PHP
101
HTML
11
JavaScript
77
XML
2
CSS
5
SQL
13
JSP
2
C#
1
ASP.NET
0
Submit a Code Snippet
Blog Archive
September 2007
August 2007
July 2007
June 2007
May 2007
November 2006
October 2006
Search Archives
Random Affiliates
Tom Morris
ReviewMe!
Uno-Code
BioMetric Base
Want to become an affiliate?
Read more
...
Privacy Policy
© 2012
Snippet Library
SQL - Structured Query Language (.sql)
Dynamic SQL in StoredProc
Build a SQL statement in a stored proceedure, and execute it
CREATE PROCEDURE dbo.bcReportsOMList @state char(5), @sort varchar(10) AS DECLARE @sql varchar(7000) set @sql = ('SELECT bcOfficialMeasurers.OMID, bcUserMain.CustomerID, bcOfficialMeasurers.OMStartDate, bcOfficialMeasurers.OMEndDate, bcOfficialMeasurers.OMNotes, bcOfficialMeasurers.UserMainID, bcOfficialMeasurers.TrainingTypeID, bcUserMain.UserMainID, bcUserMain.UserFName, bcUserMain.UserLName, bcUserMain.UserTitle, bcAddresses.Address, bcAddresses.City, bcAddresses.State, bcAddresses.Zip, bcAddresses.UserMainID, bcPhoneNumbers.UserMainID, bcPhoneNumbers.HomeNumber, bcPhoneNumbers.EmailAddress, bcMenuOMTrainingTypes.TrainingTypeID, bcMenuOMTrainingTypes.TrainingTypeDescription FROM ((((bcUserMain INNER JOIN bcOfficialMeasurers ON bcUserMain.UserMainID = bcOfficialMeasurers.UserMainID) INNER JOIN bcAddresses ON bcUserMain.UserMainID = bcAddresses.UserMainID) INNER JOIN bcPhoneNumbers ON bcUserMain.UserMainID = bcPhoneNumbers.UserMainID) INNER JOIN bcMenuOMTrainingTypes ON bcOfficialMeasurers.TrainingTypeID = bcMenuOMTrainingTypes.TrainingTypeID) ') IF @state <> 'all' BEGIN set @sql = @sql +( ' WHERE bcAddresses.State = ' ) set @sql = @sql + '''' +( rtrim(@state) ) + '''' END IF @sort = 'ByName' set @sql = @sql +( ' ORDER BY bcUserMain.UserLName, bcUserMain.UserFName ' ) ELSE set @sql = @sql +(' ORDER BY bcOfficialMeasurers.OMID' ) execute (@sql) RETURN GO
Go Back
Advertisements