condition, then run the query at the end, at least that's how I do it:
DECLARE @selectString NVARCHAR(100)
set @selectString='select ID, name, orderNumb from '
if @tablename = 1
set @[EMAIL PROTECTED] + 'bob'
else
set @[EMAIL PROTECTED] + 'frank'
EXEC sp_executesql @[EMAIL PROTECTED]
Jacob
_____
From: Kelly Matthews [mailto:[EMAIL PROTECTED]
Sent: Friday, September 10, 2004 5:31 PM
To: SQL
Subject: SP IF help
Why wont something like this work? is there a way to do something like this
in a stored procedure?
CREATE PROCEDURE [dbo].[fa_users_inout]
AS
DECLARE @sortfield int
DECLARE @sorttype varchar(4)
SELECT
a.LastName
, a.FirstName
, a.UserName
, a.Password
, a.ID
, a.ActiveStatus
, a.ClientDisplayable
, b.Location as location
, c.userlevel as newuserlevel
FROM
abPartnerUsers a INNER
JOIN abPartnerLocation b ON a.LocationID = b.ID
JOIN faPermissions c ON a.PermissionsID = c.ID
ORDER BY
IF @sortfield = 1
BEGIN
LastName @sorttype, FirstName
END
ELSE IF @sortfield = 2
BEGIN
FirstName @sorttype, LastName
END
ELSE IF @sortfield = 3
BEGIN
username @sorttype, LastName, FirstName
END
ELSE IF @sortfield = 4
BEGIN
password @sorttype, LastName, FirstName
END
ELSE IF @sortfield = 5
BEGIN
location @sorttype, LastName, FirstName
END
ELSE IF @sortfield = 6
BEGIN
newuserlevel @sorttype, LastName, FirstName
END
ELSE IF @sortfield = 7
BEGIN
ActiveStatus @sorttype, LastName, FirstName
END
ELSE
BEGIN
LastName desc
END
GO
_____
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]
