Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have values in two tables

ID Name Address
1 Moses NGL


ID Pass Expiry UserID
1 ABC 26/11/14 1
2 EFG 29/12/14 1


I Need an query with the structure

ID Name Address Pass1 Expiry1 Pass2 Expiry2
1 Moses NGL ABC 26/11/14 EFG 29/12/14

Is there any way to do that
Posted

Just a simple JOIN[^] !!!
 
Share this answer
 
Comments
CPallini 20-Oct-14 5:49am    
5.
Thanks a lot CPallini. :)
Rajesh waran 20-Oct-14 6:14am    
My 5
Thanks Gopu Raj. :)
Rajesh waran 20-Oct-14 6:47am    
Welcome.
Try these, May be it will helpfull.

SQL
--create table sampled1(id int,name varchar(40),address varchar(100))
--create table sampled2(id int, pass varchar(40),expiry datetime,userid int)

--insert into sampled1 values(1,'moses','ngl')
--insert into sampled2 values(1,'abc','1-1-2014',1)
--insert into sampled2 values(2,'EF','1-1-2014',1)


SQL
SELECT id,dbo.fn_GetHandlingCodes ()
FROM sampled2 where id = 1




alter FUNCTION dbo.fn_GetHandlingCodes ()
RETURNS VARCHAR(1200)
AS
BEGIN
DECLARE @HandlingCode VARCHAR(20)
DECLARE @HandlingCode1 VARCHAR(20)
DECLARE @ReturnValue VARCHAR(4000)

-- use that fastest cursor methods: local fast_forward
DECLARE code_cursor CURSOR LOCAL fast_forward FOR
SELECT pass,expiry
FROM sampled2
SET @ReturnValue = '' -- set to non null

OPEN code_cursor
FETCH NEXT FROM code_cursor INTO @HandlingCode,@HandlingCode1
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @ReturnValue = @ReturnValue + @HandlingCode + ', ' + @HandlingCode1 +', '

IF LEN (@ReturnValue) > 1000 BREAK -- avoid overflow

FETCH NEXT FROM code_cursor INTO @HandlingCode,@HandlingCode1
END

CLOSE code_cursor
DEALLOCATE code_cursor

-- remove last delimiter
IF LEN(@ReturnValue) > 1 SET @ReturnValue = SUBSTRING(@ReturnValue,1,LEN(@ReturnValue)-2)

RETURN @ReturnValue
END
 
Share this answer
 
Comments
Moses Geo 20-Oct-14 9:46am    
Thank you for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900