473,383 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

insert into ... select ... from ...where...in

I have this in my stored procedure:

SET @CatInsert = 'INSERT INTO tbl_shopscats (shopID, categoryID) SELECT ' +
CONVERT(varchar,@shopID) + ', categoryID From tbl_categories Where
categoryID IN (' + @categoryID + ')'

@categoryID is a string of ID's separated by comma's. ex: "1,25,78"

So if @shopID = 63

We should have these records inserted:
record1: shopID= 63 and categoryID=1
record2: shopID= 63 and categoryID=25
record3: shopID= 63 and categoryID=78

The problem is that it only insert when the categoryID is "1".
The second problem is that it also inserts "1" if the ID is for ex. "124"

If there is no "1" in the string, nothing is inserted. ex: when @categoryID
= "25,78"

Any ideas ?
Thx,
Nic
Nov 18 '05 #1
6 4102
If you go into the master database and execute:
--
MY ASP.Net tutorials
http://www.openmymind.net/
"nicholas" <mu********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have this in my stored procedure:

SET @CatInsert = 'INSERT INTO tbl_shopscats (shopID, categoryID) SELECT ' + CONVERT(varchar,@shopID) + ', categoryID From tbl_categories Where
categoryID IN (' + @categoryID + ')'

@categoryID is a string of ID's separated by comma's. ex: "1,25,78"

So if @shopID = 63

We should have these records inserted:
record1: shopID= 63 and categoryID=1
record2: shopID= 63 and categoryID=25
record3: shopID= 63 and categoryID=78

The problem is that it only insert when the categoryID is "1".
The second problem is that it also inserts "1" if the ID is for ex. "124"

If there is no "1" in the string, nothing is inserted. ex: when @categoryID = "25,78"

Any ideas ?
Thx,
Nic

Nov 18 '05 #2
bah..sorry about that,
anyways, if you go into your master database and execut:

declare @sql nvarchar(1024)
declare @query varchar(128)
set @query = '5,10,11'
set @sql = 'SELECT dbid from sysdatabases where dbid in (' + @query + ')'
exec sp_executesql @sql

you'll see that it works as expected... you might want to break down your
query into something like that and try and see what it is you are doing
wrong. Our queries look almost the same except \
(a) I don't know how you are executing @CatInsert
(b) Your query is slightly more complex (try removing the insert and see if
the select works on its own like mine)
(c) I'm not sure of the exec value of @categoryID (though I can't imagine
it's any different than my @query)

On a side note, this is ugly and dangerous code (sql injection)...consider
alternatives, such as a UDF to convert @categoryId into a table datatype

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
If you go into the master database and execute:
--
MY ASP.Net tutorials
http://www.openmymind.net/
"nicholas" <mu********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have this in my stored procedure:

SET @CatInsert = 'INSERT INTO tbl_shopscats (shopID, categoryID) SELECT '
+
CONVERT(varchar,@shopID) + ', categoryID From tbl_categories Where
categoryID IN (' + @categoryID + ')'

@categoryID is a string of ID's separated by comma's. ex: "1,25,78"

So if @shopID = 63

We should have these records inserted:
record1: shopID= 63 and categoryID=1
record2: shopID= 63 and categoryID=25
record3: shopID= 63 and categoryID=78

The problem is that it only insert when the categoryID is "1".
The second problem is that it also inserts "1" if the ID is for ex.

"124"
If there is no "1" in the string, nothing is inserted. ex: when

@categoryID
= "25,78"

Any ideas ?
Thx,
Nic


Nov 18 '05 #3
I don't think you states any variable to insert though

chanmm

"nicholas" <mu********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have this in my stored procedure:

SET @CatInsert = 'INSERT INTO tbl_shopscats (shopID, categoryID) SELECT '
+
CONVERT(varchar,@shopID) + ', categoryID From tbl_categories Where
categoryID IN (' + @categoryID + ')'

@categoryID is a string of ID's separated by comma's. ex: "1,25,78"

So if @shopID = 63

We should have these records inserted:
record1: shopID= 63 and categoryID=1
record2: shopID= 63 and categoryID=25
record3: shopID= 63 and categoryID=78

The problem is that it only insert when the categoryID is "1".
The second problem is that it also inserts "1" if the ID is for ex. "124"

If there is no "1" in the string, nothing is inserted. ex: when
@categoryID
= "25,78"

Any ideas ?
Thx,
Nic

Nov 18 '05 #4
Thanks to all of you, but I changed my code completely.
It indeed wasn't a good way for doing this.
Everything works fine now...and I think it's safer too.

Again thank you,
Nic

"nicholas" <mu********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
I have this in my stored procedure:

SET @CatInsert = 'INSERT INTO tbl_shopscats (shopID, categoryID) SELECT ' + CONVERT(varchar,@shopID) + ', categoryID From tbl_categories Where
categoryID IN (' + @categoryID + ')'

@categoryID is a string of ID's separated by comma's. ex: "1,25,78"

So if @shopID = 63

We should have these records inserted:
record1: shopID= 63 and categoryID=1
record2: shopID= 63 and categoryID=25
record3: shopID= 63 and categoryID=78

The problem is that it only insert when the categoryID is "1".
The second problem is that it also inserts "1" if the ID is for ex. "124"

If there is no "1" in the string, nothing is inserted. ex: when @categoryID = "25,78"

Any ideas ?
Thx,
Nic

Nov 18 '05 #5
Nic,

If @categoryID is a parameter to your stored procedure, make sure
you're defining the parameter length. The procedure:

CREATE PROCEDURE nicholas
@shopID INT,
@categoryID VARCHAR
AS ...

will give @categoryID a length of 1 (see Books Online / Transact SQL
Reference / char and varchar). This explains the behavior where your
"1,25,78" and "124" parameters are acting like "1". As for nothing
happening with "25,78", my guess is it's being truncated to "2" and
you don't have a tbl_categories.categoryID of 2?

At any rate, you need to declare a comfy size for your @categoryID
parameter. Max is 8,000. I don't know if you pay any penalty for
declaring 8000, as Books Online claims that storage size is the actual
string size, not the declared string size (lurkers please correct if
I'm wrong; thanks!).

Sooo, try the following instead:

CREATE PROCEDURE nicholas
@shopID INT,
@categoryID VARCHAR(8000)
AS ...
I'll also give the usual warning about jamming a list into a string:
if you think you'll *ever* need more than 8K characters for the list
of category ID's, consider another approach.

Hope this helps,
Ed
Nov 18 '05 #6
Nic,

I posted a reply earlier that seems to have gone south...

At any rate, I'm guessing the problem is that you declared @categoryID
as VARCHAR without a length specifier. When you do that, the length is
defaulted to 1 (see Books Online / Transact SQL Reference / char and
varchar). That would explain "1,25,78" and "124" acting like "1". As
for "25,78" doing nothing, perhaps you don't have a
tbl_categories.categoryid of "2"?

I don't know where @categoryID comes from, but if it's a parameter to
the SP you need to go from this:

CREATE PROCEDURE whatever @categoryID VARCHAR

to this:

CREATE PROCEDURE whatever @categoryID VARCHAR(8000)

The same default length of 1 also applies to DECLARE statements.

8000 is the max VARCHAR length, and as far as I can tell you don't pay
a penalty for declaring the max length (according to Books Online,
storage is actual size rather than declared size, but I could be
wrong; lurkers welcome to weigh in).

Finally, I'll give the standard warning about representing a list in a
string: if you think you'll *ever* need more than 8000 characters to
represent the list, consider a different approach.

Hope this helps,
Ed
Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
0
by: jtocci | last post by:
I'm having a big problem with CREATE RULE...ON INSERT...INSERT INTO...SELECT...FROM...WHERE when I want to INSERT several (20~50) records based on a single INSERT to a view. Either I get a 'too...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
4
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why...
20
by: Mark Harrison | last post by:
So I have some data that I want to put into a table. If the row already exists (as defined by the primary key), I would like to update the row. Otherwise, I would like to insert the row. I've...
9
by: rhaazy | last post by:
Using MS SQL 2000 I have a stored procedure that processes an XML file generated from an Audit program. The XML looks somewhat like this: <ComputerScan> <scanheader>...
6
by: lenygold via DBMonster.com | last post by:
Hi everybody: What is the best way to I have 10 tables with similar INSERT requiremnts. INSERT INTO ACSB.VAATAFAE WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS ( SELECT AA_TIN,...
2
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a...
1
by: EJO | last post by:
with sql 2000 enterprise Trying to build a stored procedure that will take the rows of a parent table, insert them into another table as well as the rows from a child table to insert into...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.