Re: Copy Row Of Data From Table to Table In Same DB



The syntax error is because of the commas in the WHERE clause. The
conditions in the WHERE clause are logical expressions and you have to use
AND or OR between expressions based on what you need to filter. A trimmed
down example is:

INSERT INTO Table1
(column1,
column2)
SELECT column1,
column2
FROM Table2
WHERE column1 = @column1
AND column2 = @column2

All that said, I am a bit puzzled why you decided to write this stored
procedure and the purpose of passing those column parameters. If you just
need to copy the Table2 to Table1, then directly run the statement like
this:

INSERT INTO Table1
(column1,
column2,
-- ... the rest of the columns go here
column17)
SELECT column1,
column2,
-- ... the rest of the columns go here
column17
FROM Table2

And then if you have any filters that you need to apply to the columns from
Table2, you can add the WHERE clause. Also, you could wrap that statement in
a stored procedure, but I just do not see the purpose of passing all those
column parameters to the SP. Can you explain why you added them and how you
plan to execute the SP, and maybe an example of what parameters you pass?

If you are trying to perform something like dynamic searching (that is
filter on multiple variable conditions), then you may want to read Erland
Sommarskog's article on dynamic search conditions:
http://www.sommarskog.se/dyn-search.html

HTH,

Plamen Ratchev
http://www.SQLStudio.com


.



Relevant Pages

  • Re: deleting duplicates
    ... There is none (but see below form some kludges). ... then any WHERE clause that matches one will match the other as ... (Column1 int NOT NULL, ... FROM MyTableTMP ...
    (microsoft.public.sqlserver.mseq)
  • Re: Where clause in DataTable.Select method
    ... What does 'From Column1' mean? ... And the table is always going to be the DataTable you are calling ... So the WHERE clause you have in the statement is already what you need. ... > Just curious how I would include a WHERE clause in the filter string of ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Copy Row Of Data From Table to Table In Same DB
    ... conditions in the WHERE clause are logical expressions and you have to use ... AND or OR between expressions based on what you need to filter. ... SELECT column1, ... As for the SPROC decision, I decided to use a SPROC because of the ...
    (comp.databases.ms-sqlserver)