Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Express Edition and Compact Edition (2005)
 INSERT INTO

Author  Topic 

programer
Posting Yak Master

221 Posts

Posted - 2010-08-08 : 06:56:25
What is wrong with this code, because it refuses to stop and continued on until you click Stop?

DECLARE @pos int, @curruntLocation char(20), @input varchar(2048)
SELECT @pos=0
SELECT @input = '1234,2345,3456'
SELECT @input = @input + ','


WHILE CHARINDEX(',',@input) > 0
BEGIN
SELECT @pos=CHARINDEX(',',@input)
SELECT @curruntLocation = RTRIM(SUBSTRING(@input,1,@pos-1))
INSERT INTO TABLE_1 (id_uporabnika) VALUES (@curruntLocation)
--SELECT @input=SUBSTRING(@input,@pos+1,2048)
END

SELECT * FROM TABLE_1

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 07:37:33
your while loop is running endlessly because your
condition:
charindex(',',@input) > 0

is always bigger than zero and it keeps inserting values.

you ought to so something with your input or your condition.
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2010-08-08 : 07:53:21
quote:
Originally posted by slimt_slimt

your while loop is running endlessly because your
condition:
charindex(',',@input) > 0

is always bigger than zero and it keeps inserting values.

you ought to so something with your input or your condition.





thanks.

With this code I would like to value: '1234, 2345.3456 "in the same column, write to:

TABLE_1:
ID_uporabnika:
1234
2345
3456

Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 08:15:28
try this:

declare @temp table (id_uporabnika varchar(50))

declare @input varchar(200) set @input = '1234,2345,3456,12,31415,435'

if right(rtrim(@input),1) <> ',' set @input = @input + ','

declare @stevc smallint
declare @del_stringa varchar(50)

set @stevc = patindex('%,%',@input)
while @stevc <> 0
begin
set @del_stringa = left(@input, @stevc-1)
insert into @temp
select @del_stringa
set @input = stuff(@input,1,@stevc,'')
set @stevc = patindex('%,%',@input)
end
select * from @temp
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2010-08-08 : 08:39:43
quote:
Originally posted by slimt_slimt

try this:

declare @temp table (id_uporabnika varchar(50))

declare @input varchar(200) set @input = '1234,2345,3456,12,31415,435'

if right(rtrim(@input),1) <> ',' set @input = @input + ','

declare @stevc smallint
declare @del_stringa varchar(50)

set @stevc = patindex('%,%',@input)
while @stevc <> 0
begin
set @del_stringa = left(@input, @stevc-1)
insert into @temp
select @del_stringa
set @input = stuff(@input,1,@stevc,'')
set @stevc = patindex('%,%',@input)
end
select * from @temp




thanks works

declare @Table_1 table (id_uporabnika varchar(50))

declare @input varchar(200) set @input = '1234,2345,3456,12,31415,435'

if right(rtrim(@input),1) <> ',' set @input = @input + ','

declare @stevc smallint
declare @del_stringa varchar(50)

set @stevc = patindex('%,%',@input)
while @stevc <> 0
begin
set @del_stringa = left(@input, @stevc-1)
insert into @Table_1
select @del_stringa
set @input = stuff(@input,1,@stevc,'')
set @stevc = patindex('%,%',@input)
end
select * from @Table_1

These data would have liked to have still stored.
If you look Table_1 find that this information is not saved.
How do I save them forever?

Thanks!
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 08:53:59
instead of using temporary table (@temp), you should use a normal table. so use this to have your data saved forever.

Create table Table_1 (id_uporabnika varchar(50))
declare @input varchar(200) set @input = '1234,2345,3456,12,31415,435'

if right(rtrim(@input),1) <> ',' set @input = @input + ','

declare @stevc smallint
declare @del_stringa varchar(50)

set @stevc = patindex('%,%',@input)
while @stevc <> 0
begin
set @del_stringa = left(@input, @stevc-1)
insert into Table_1
select @del_stringa
set @input = stuff(@input,1,@stevc,'')
set @stevc = patindex('%,%',@input)
end
select * from Table_1
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2010-08-08 : 09:05:37
quote:
Originally posted by slimt_slimt

instead of using temporary table (@temp), you should use a normal table. so use this to have your data saved forever.

Create table Table_1 (id_uporabnika varchar(50))
declare @input varchar(200) set @input = '1234,2345,3456,12,31415,435'

if right(rtrim(@input),1) <> ',' set @input = @input + ','

declare @stevc smallint
declare @del_stringa varchar(50)

set @stevc = patindex('%,%',@input)
while @stevc <> 0
begin
set @del_stringa = left(@input, @stevc-1)
insert into Table_1
select @del_stringa
set @input = stuff(@input,1,@stevc,'')
set @stevc = patindex('%,%',@input)
end
select * from Table_1





My mistake.
It worked fine, we had to do refresh.

Thanks!
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 09:10:10
(y)
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2010-08-08 : 10:53:10
quote:
Originally posted by slimt_slimt

(y)



Ok, this is work.

In what way is choose, in any field they go?

Eg: How do I set the variable value?

id_uporabnika varchar (50)), this is one column.

If I have:
TextBox1 = 1234
TextBox2 = 2345
TextBox3 = 3456
TextBox4 = 12
TextBox5 = 31415
TextBox1 = 435

Or anything else ...


How do I set: @input = '1234, 2345,3456,12,31415,435 '

of variables, data will be written to the table?

Using Stored procedure.

thanks.
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 11:02:06
i don't understand what do you want here?
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2010-08-08 : 11:19:22
quote:
Originally posted by slimt_slimt

i don't understand what do you want here?




If I have only one column in which I get all the information:

Example:

----- Nickname Empty table

TextBox1: Text = nickname1
TextBox2.Text = nickname2
TextBox3.Text = nickname3

Instead '1234, 2345,3456,12,31415,435 ', I here set the name field


Nickname Table
nickname1
nickname2
nickname3
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 12:45:51
still no clue? sorry.
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2010-08-08 : 13:03:43
quote:
Originally posted by programer

quote:
Originally posted by slimt_slimt

i don't understand what do you want here?




If I have only one column in which I get all the information:

Example:

----- Nickname Empty table

TextBox1: Text = nickname1
TextBox2.Text = nickname2
TextBox3.Text = nickname3

Instead '1234, 2345,3456,12,31415,435 ', I here set the name field


Nickname Table
nickname1
nickname2
nickname3




They prepared me code in which the values are: '1234, 2345,3456,12,31415,435 '


How to replace values: 1234 ...... define its value.

TextBox1.Text = programer.

So it looks:
'programer 2345,3456,12,31415,435'
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-08 : 14:06:20
Either you solve this issue on VB level (or your programming language level)
or you insert this values into a table and load your string/array with each values separately from the table.
Go to Top of Page
   

- Advertisement -