If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > please help me to correct its infinity loop(Dynamic Create Table in Stored Procedure)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-06-05, 15:27
DonAmir DonAmir is offline
Registered User
 
Join Date: Nov 2005
Posts: 5
Lightbulb please help me to correct its infinity loop(Dynamic Create Table in Stored Procedure)

I wrote it finally , but it has an unwanted infinity loop , please help me to correct its infinity loop :
Code:
CREATE PROCEDURE [dbo].[CreateMyTable] ( @FieldsNameStr varchar(2000),
          @FieldsTypeStr varchar(2000),
         @TableName varchar(2000) )


AS
declare  @i int
declare  @j int
declare  @TempStr varchar(200)
declare  @TempStr1 varchar(200)
declare  @TempStr2 varchar(200)



set @i = 0
set @j = 0
set @TempStr = ''

WHILE (@i < Len(@FieldsNameStr)) or (@j < Len(@FieldsTypeStr))
begin

  set @TempStr1 = ''
  while  not(substring(@FieldsNameStr, @i, 1) = ',') or 
    (@i < Len(@FieldsNameStr))
  begin
    set @TempStr1 = @TempStr1 + substring(@FieldsNameStr, @i, 1)
    set @i = @i +1
  end


  set @TempStr2 = ''
  while  not(substring(@FieldsTypeStr, @j, 1) = ',') or 
    (@i < Len(@FieldsTypeStr))
  begin
    set @TempStr2 = @TempStr2 + substring(@FieldsTypeStr, @j, 1)
    set @j = @j +1
  end

  set @TempStr = @TempStr + '[' +@TempStr1+ ']' + @TempStr2 +' NOT NULL ,'
  set @i = @i +1
  set @j = @j +1
end


EXEC('CREATE TABLE [dbo].[' + @TableName + '] ( '+
          @TempStr +	
         ') ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]')
GO
Reply With Quote
  #2 (permalink)  
Old 11-07-05, 02:52
ivon ivon is offline
Registered User
 
Join Date: Nov 2002
Posts: 272
It's looks like T-SQL (SQL Server), and it looks like you are looking for the first occurrence of a ',' in your strings.
If both are the case, have a look at the CHARINDEX function. It can give you the exact location of the comma, so you can get rid of some of the loops.
Reply With Quote
  #3 (permalink)  
Old 11-07-05, 02:54
ivon ivon is offline
Registered User
 
Join Date: Nov 2002
Posts: 272
Oh, and looking at your code a bit closer:
while not(substring(@FieldsTypeStr, @j, 1) = ',') or
(@i < Len(@FieldsTypeStr))
That @i most likely needs to be @j.
Reply With Quote
  #4 (permalink)  
Old 11-07-05, 07:22
DonAmir DonAmir is offline
Registered User
 
Join Date: Nov 2005
Posts: 5
Quote:
Originally Posted by ivon
It's looks like T-SQL (SQL Server), and it looks like you are looking for the first occurrence of a ',' in your strings.
If both are the case, have a look at the CHARINDEX function. It can give you the exact location of the comma, so you can get rid of some of the loops.
I checkede it on Books Online and found CHARINDEX used in column strings not in strings! Am I right?
Reply With Quote
  #5 (permalink)  
Old 11-07-05, 07:25
DonAmir DonAmir is offline
Registered User
 
Join Date: Nov 2005
Posts: 5
Quote:
Originally Posted by ivon
Oh, and looking at your code a bit closer:
while not(substring(@FieldsTypeStr, @j, 1) = ',') or
(@i < Len(@FieldsTypeStr))
That @i most likely needs to be @j.
Thanks , as you said I changed it but it didn't make any diffrence.
Reply With Quote
  #6 (permalink)  
Old 11-07-05, 08:07
ivon ivon is offline
Registered User
 
Join Date: Nov 2002
Posts: 272
CHARINDEX does work on varchar variables:
Code:
declare @i int
declare @a varchar(1)
declare @b varchar(6)

set @a = 'B'
set @b = 'ABCDEF'

set @i = CHARINDEX(@a,@b)
select @i
works for me.

Also, I notice that your @TempStr will end in a comma. So even if we manage to solve the infinity thing, the EXEC command will fail.
Reply With Quote
  #7 (permalink)  
Old 11-07-05, 08:15
ivon ivon is offline
Registered User
 
Join Date: Nov 2002
Posts: 272
I think I found another problem:

while
not(substring(@FieldsNameStr, @i, 1) = ',')
or
(@i < Len(@FieldsNameStr))

If a comma is encountered, the second of your condition will still be true. So your inner loop will not end; at least not before the end of @FieldsNameStr is reached.
Reply With Quote
  #8 (permalink)  
Old 11-07-05, 16:25
DonAmir DonAmir is offline
Registered User
 
Join Date: Nov 2005
Posts: 5
I changed it & corrected its loop , it works for one column but it has problem about this part for more than 1:
EXEC
('CREATE TABLE [dbo].[' + @TableName + '] ( '+ @TempStr +') ')


Code:
CREATE PROCEDURE [dbo].[CreateMyTable] ( @FieldsNameStr varchar(2000),
                                                                             @FieldsTypeStr varchar(2000),
                                                                             @TableName varchar(2000) )


AS
declare  @i int
declare  @j int
declare  @TempStr varchar(200)
declare  @TempStr1 varchar(200)
declare  @TempStr2 varchar(200)


set @i = -1
set @j = -1
set @TempStr = ''

WHILE (@i < Len(@FieldsNameStr)) and (@j < Len(@FieldsTypeStr))
begin

  set @TempStr1 = ''
  declare @tt varchar(1)
   set @tt=substring(@FieldsNameStr, @i, 1)

  while  not(@tt = ',') and (@i < Len(@FieldsNameStr))
  begin
    set @i = @i +1
    set @TempStr1 = @TempStr1 + substring(@FieldsNameStr, @i, 1)
--    print @TempStr1+'0'
  end


  set @TempStr2 = ''
  declare @rr varchar(1)
  set @rr=substring(@FieldsTypeStr, @j, 1)
  while  not(@rr= ',') and (@j < Len(@FieldsTypeStr))
  begin
   set @j = @j +1  
   set @TempStr2 = @TempStr2 + substring(@FieldsTypeStr, @j, 1)
--   print @TempStr2+'1'
  end

  set @TempStr = @TempStr + '[' +@TempStr1+ ']' + @TempStr2 +' NOT NULL ,'
--  print @TempStr+'3'
  set @i = @i +1
  set @j = @j +1
end


EXEC
  ('CREATE TABLE [dbo].[' + @TableName + '] ( '+ @TempStr +') ')
GO
Reply With Quote
  #9 (permalink)  
Old 11-08-05, 09:16
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
Why don't yo supply us with what the unput is suppose to be, and what the @TableName and @TempString are suppose to be....


And you'd probably be better off posting this in the SQL Server forum
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #10 (permalink)  
Old 11-09-05, 03:01
ivon ivon is offline
Registered User
 
Join Date: Nov 2002
Posts: 272
Quote:
Originally Posted by DonAmir
I changed it & corrected its loop , it works for one column but it has problem about this part for more than 1:
EXEC
('CREATE TABLE [dbo].[' + @TableName + '] ( '+ @TempStr +') ')
As I wrote before, @TempStr ends in a comma. Frankly, I'm surprised it works for one column.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On