Solved

why the output not shown

Posted on 2013-01-02
9
240 Views
Last Modified: 2013-01-25
I have a MS  SQL 2008 script to try to print out an output from a table for each row. But it gave me * output. I compile the code, I don't see an error. can someone give me advice how to fix it?
0
Comment
Question by:wasabi3689
9 Comments
 

Author Comment

by:wasabi3689
ID: 38738590
Attaching the source script here

DECLARE @device_id varchar(6);
DECLARE @counter decimal(10,0);
DECLARE @device_count INT;
DECLARE @lgroup_id varchar(20);

SET @counter = 1;
SET @device_count = 2;
SET @lgroup_id=6;

DECLARE devices_my CURSOR LOCAL for
  select device_id from devices where DEVICE_PIN between  5009166661 and 5009166662 order by device_id;

open devices_my;

SET IDENTITY_INSERT SCHED_ITEMS ON


while @COUNTER <= @device_count    
begin
    FETCH NEXT FROM devices_my INTO @device_id;
    IF @@FETCH_STATUS = 0

    PRINT 'Device ID: ' + @device_id + '    Counter: ' + convert(varchar(10),@counter)
   
    SET @COUNTER = @COUNTER + 1
end
CLOSE devices_my;
DEALLOCATE devices_my;

SET IDENTITY_INSERT SCHED_ITEMS OFF;
0
 
LVL 8

Expert Comment

by:venk_r
ID: 38738665
Is this the full code attached?Whats the output your'e getting.Did you say you just getting '*'
as output?
0
 

Author Comment

by:wasabi3689
ID: 38738683
yes, this is the full code, the output I got is

*
*
0
 
LVL 8

Accepted Solution

by:
venk_r earned 500 total points
ID: 38738697
Your'e missing
 FETCH NEXT FROM devices_my INTO @device_id;
before the while loop starts.You need to mention the above before the while loop starts.
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:wasabi3689
ID: 38738716
Do you mean I should put this way instead?


FETCH NEXT FROM devices_my INTO @device_id;

while @COUNTER <= @device_count    
begin
   
    IF @@FETCH_STATUS = 0

    PRINT 'Device ID: ' + @device_id + '    Counter: ' + convert(varchar(10),@counter)
          

    SET @COUNTER = @COUNTER + 1
end
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38738720
Is there any reason you are writing the CURSOR that way and why are you doing the following?
SET IDENTITY_INSERT SCHED_ITEMS ON
,,,
SET IDENTITY_INSERT SCHED_ITEMS OFF;
0
 
LVL 8

Expert Comment

by:venk_r
ID: 38738722
Yes please .See below.
Also check to see if @device_id has NULL VALUE or any special character in it.

FETCH NEXT FROM devices_my INTO @device_id;

while @COUNTER <= @device_count    
begin
   
    IF @@FETCH_STATUS = 0

    PRINT 'Device ID: ' + @device_id + '    Counter: ' + convert(varchar(10),@counter)
         
   SET @COUNTER = @COUNTER + 1
 FETCH NEXT FROM devices_my INTO @device_id;

end
0
 

Author Comment

by:wasabi3689
ID: 38738743
print is just test statement. I have "Insert into..." statement instead.
0
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 38739184
Change your statement like this....

DECLARE devices_my CURSOR LOCAL for
  select cast(device_id  as varchar(200))  from devices where DEVICE_PIN between  5009166661 and 5009166662 order by device_id;



I think there is some issue with implicit conversion...
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now