Solved

why the output not shown

Posted on 2013-01-02
9
246 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

740 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