?
Solved

why the output not shown

Posted on 2013-01-02
9
Medium Priority
?
250 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 8

Accepted Solution

by:
venk_r earned 2000 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

770 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