Please help me out to get this issue solve. Am don't know, how to get the output.

This is the way, i need. But i don't know.
(a)select distinct empid from emptable where year=2011 order by empid
(b)I needs to pass this empid with the while loop:

      select count from empdetails where year=2011 and empid=?
            'print statement

How can i write a query using Transact-SQL. Please suggest me.

Recommended Answers

All 7 Replies

Get the output of query (a) to a temp table with an identity column as RowID

declare @EmpIds table
  RowID identity(1,1) int,
  empid varchar(20)
declare @count int

select @count = max(RowID) from @EmpIds 
while (@count > 0) 
select @empid = empid from @EmpIds where RowID = @count
// do the operations here  
select @count = @count - 1

Hi, thanks for your query. I can't understand your query. Can you post it the query with some clear explanation.

Getting, the count of the total records and till looping to "Zero" count, the while loop executes. How can i pass the empid(emptable) to the inner table empdetails. Please can you explain it

Suppose this query

select distinct empid from emptable where year=2011 order by empid

returns two records--> Emp1 and Emp2.

After the temp table population, @EmpIds will contain

----- ------
1 Emp1
2 Emp2

SELECT @count = max(RowID) FROM @EmpIds

The above query will return @count = 2

In the while loop,

SELECT @empid = empid FROM @EmpIds WHERE RowID = 2

The above query will return @empid = Emp2

Use this @empid in your query

select count from empdetails where year=2011 and empid= @empid

Hope this is what you are looking for.


Thanks for your steps. I close to my output. How can i create the stored procedure with passing parameters. Thanks once more

@Parameter DataType

// Stored Procedure Contents

how to run the stored procedure is not running. Shows the error message on running stored procedure.

exec dbo.procname

What is the error message?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.