Click here to Skip to main content
15,921,276 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have the following codes in my SQL. All things are ok. The value can be retrieved from the table of database but how can i set the value(count as in below) to a select statement and display it out?


SQL
CREATE TABLE #tempStaTable
	(sta0 VARCHAR(50), sta1 VARCHAR(50), sta2 VARCHAR(50), sta3 VARCHAR(50), sta4 VARCHAR(50), sta5 VARCHAR(50), sta6 VARCHAR(50), sta7 VARCHAR(50))
	
	SET @TABLE = '#tempStaTable'
	
	SET @cursor = CURSOR FOR
	SELECT dbo.CRF_Project.DocStatus
	FROM vsCustomizationRequest.dbo.CRF_Project
	WHERE CustomerID = '1'
	--GROUP BY CustomerID, DocStatus
	
	OPEN @cursor
	FETCH NEXT
	FROM @cursor into @status
	WHILE @@FETCH_STATUS = 0
	BEGIN
		--HERE
		--PRINT @status
		IF(@status = 0)
			SET @COUNT = @COUNT + 1
			SET @varCount = CONVERT(VARCHAR(50), @COUNT)
			SET @ColumnName = 'sta0'
			SET @statement = 'UPDATE #tempStaTable SET '+@ColumnName+' = '+@varCount+''
			EXEC (@statement)
			
		IF(@status = 1)
			SET @COUNT1 = @COUNT1 + 1
		IF(@status = 2)
			SET @COUNT2 = @COUNT2 + 1
		IF(@status = 3)
			SET @COUNT3 = @COUNT3 + 1
		IF(@status = 4)
			SET @COUNT4 = @COUNT4 + 1
		IF(@status = 5)
			SET @COUNT5 = @COUNT5 + 1
		IF(@status = 6)
			SET @COUNT6 = @COUNT6 + 1
		IF(@status = 7)
			SET @COUNT7 = @COUNT7 + 1
						
		FETCH NEXT
		FROM @cursor into @status
		
	END
	PRINT @COUNT
	PRINT @COUNT1
	PRINT @COUNT2
	PRINT @COUNT3
	PRINT @COUNT4
	PRINT @COUNT5
	PRINT @COUNT6
	PRINT @COUNT7
	
	CLOSE @cursor
	DEALLOCATE @cursor
	
        --the problem is at here, i cant display the count value
	SELECT * FROM #tempStaTable
Posted

you can set @Count1 to @Count7 as a global variable and set the value in forloop cursor.

then use PRINT 'Count1' + convert(varchar(7),@Count1) like this.
 
Share this answer
 
Comments
Jamie888 19-May-14 4:14am    
i dont quite get it. Can u pls code some of the codes? Thks
Add the line before PRINT @COUNT

SQL
INSERT INTO #tempStaTable (sta0) VALUES(@varCount)
 
Share this answer
 

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