Solved

why the output not shown

Posted on 2013-01-02
9
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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