Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Getting Table Variable Field into Variable

Author  Topic 

shaft01
Starting Member

7 Posts

Posted - 2007-05-31 : 06:19:27
Hi All,

I'm new to writing stored procedure and I'm having trouble trying to get a table variable field into a variable so that I can run an IF statment on it.

This is it so far:-


@Client int = NULL,
@LandlordID int = NULL

AS

DECLARE @UserType TABLE
(
LandlordID int,
Client int
)

INSERT INTO @UserType EXEC CurrentUser @User


and I want the LandlordID and Client fileds to be inserted into the 2 variables so that I can run an IF statement against them to determine which select statement to run.

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-31 : 06:44:49
Post the stored procedure you wrote... or explain us clearly..

--------------------------------------------------
S.Ahamed
Go to Top of Page

shaft01
Starting Member

7 Posts

Posted - 2007-05-31 : 07:24:35
Ok, this is the stored procedure, and the table variable will only ever have one row in it.

USE [SCMdb]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[CustomerSearch]

@User varchar(20),
@NameNumb varchar(20) = NULL,
@Address varchar(20) = NULL,
@Postcode varchar(20) = NULL,
@Client int = NULL,
@LandlordID int = NULL

AS

DECLARE @UserType TABLE
(
LandlordID int,
Client int
)

INSERT INTO @UserType EXEC CurrentUser @User

@LandlordID = SELECT LandlordID FROM @UserType
@Client = SELECT Client FROM @UserType

IF (@Client = 0)
Begin
SET @LandlordID = NULL
END

IF (@NameNumb IS NULL) AND (@Address IS NULL) AND (@Postcode IS NULL)

BEGIN
WAITFOR DELAY '00:00:01'
END

ELSE

BEGIN

SELECT CustomerID, NameNumb, BillingAddress, PostalCode, Owner
FROM Customers
WHERE
(Owner = ISNULL (@LandlordID,Owner)) AND
(NameNumb Like ISNULL ('%' + @NameNumb,NameNumb) + '%') AND
(BillingAddress Like ISNULL ('%' + @Address,BillingAddress) + '%') AND
(PostalCode Like ISNULL ('%' + @Postcode,PostalCode) + '%')
ORDER BY BillingAddress, NameNumb

END


and it's this section that I don't know the syntax for

@LandlordID = SELECT LandlordID FROM @UserType
@Client = SELECT Client FROM @UserType
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-05-31 : 07:46:27
It should be like

SELECT @LandlordID = LandlordID FROM @UserType
SELECT @Client = Client FROM @UserType


--------------------------------------------------
S.Ahamed
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-05-31 : 09:25:41
quote:
Originally posted by pbguy

It should be like

SELECT @LandlordID = LandlordID FROM @UserType
SELECT @Client = Client FROM @UserType


--------------------------------------------------
S.Ahamed



Provided the query returns single value
Otherwise last returned value will be assigned

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

shaft01
Starting Member

7 Posts

Posted - 2007-05-31 : 09:32:57
It will only return a single a value. Cheers anyway it's useful to know the behaviour. Thank You
Go to Top of Page
   

- Advertisement -