Monday, March 19, 2012

INSERT table (column1, column2) (@variable, '100')

Generally, you can't use a variable in this way, correct?
INSERT table (column1, column2) VALUES (@.variable, '100')
Do I need to be learning about stored procedures to make something like
this work? Do I need to restructure the insert statement?
Thanks
-tomThat should work, otherwise use the SELCT syntax:
INSERT table (column1, column2)
SELECT @.variable, '100'
HTH, Jens Suessmeyer.|||Tom,
Yes you can use variables in a Table Insert.
create table dbo.Test
(Col1 varchar(10),
Col2 varchar(10)
)
Go
declare @.Test varchar(10)
set @.Test = 'Test'
Insert into dbo.Test (col1, col2)
Values (@.Test, 'Test2')
Go
select * From dbo.Test
Go
Drop dbo.Test
Go
HTH
Barry|||Sorry I meant ...
Drop Table dbo.Test
Go
!
Barry|||tom wrote:
> Generally, you can't use a variable in this way, correct?
> INSERT table (column1, column2) VALUES (@.variable, '100')
I don't see anything wrong with this, given that @.variable is declared and
defined.

> Do I need to be learning about stored procedures to make something
> like this work?
I don't understand the question. It should work as is. Run this script to
see:
create table #temp (
column1 int,
column2 varchar(3))
declare @.variable int
set @.variable=200
INSERT INTO #temp (column1, column2)
VALUES (@.variable, '100')
SELECT * FROM #temp
drop table #temp

> Do I need to restructure the insert statement?
>
No, what is making you think you have to?
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||Tom,
The INSERT would work but a proc would be better.
Examples to work with:
CREATE TABLE TESTTABLE101
(COLUMN1 VARCHAR(10) NOT NULL,
COLUMN2 VARCHAR(10) NOT NULL)
GO
DECLARE @.VARIABLE VARCHAR(10)
SET @.VARIABLE = 'WHATEVER'
INSERT TESTTABLE101
VALUES(@.VARIABLE, '100')
GO
SELECT * FROM TESTTABLE101
GO
CREATE PROC USP_TESTPROC
@.VARIABLE VARCHAR(10)
AS
INSERT TESTTABLE101
VALUES(@.VARIABLE, '100')
GO
EXEC USP_TESTPROC 'WHATEVER2'
GO
SELECT * FROM TESTTABLE101
GO
--DROP PROC USP_TESTPROC
--DROP TABLE TESTTABLE101
HTH
Jerry
"tom" <tomfeldsten@.hotmail.com> wrote in message
news:1129834047.688572.283250@.o13g2000cwo.googlegroups.com...
> Generally, you can't use a variable in this way, correct?
> INSERT table (column1, column2) VALUES (@.variable, '100')
> Do I need to be learning about stored procedures to make something like
> this work? Do I need to restructure the insert statement?
> Thanks
> -tom
>|||Sure, why not.
All Inserts/Updates/Deletions in databases where I work are done through
stored procedures.
There are exceptions but usually they are one-time jobs by the database DBA,
tested first on a database copy before they are committed to the production
database.
Even Selects go through SPs. Nobody has direct access to tables and queries
except the database DBA and programmers.
I bet quite a few people in the newsgroup here would agree on this method.
So, to answer your second question, YES... learn about stored procedures.
"tom" <tomfeldsten@.hotmail.com> wrote in message
news:1129834047.688572.283250@.o13g2000cwo.googlegroups.com...
> Generally, you can't use a variable in this way, correct?
> INSERT table (column1, column2) VALUES (@.variable, '100')
> Do I need to be learning about stored procedures to make something like
> this work? Do I need to restructure the insert statement?
> Thanks
> -tom
>|||> Do I need to restructure the insert statement?

>No, what is making you think you have to?
Is it possible that adding double quotes and single quotes changed the
outcome? I adding quotes thus: " ' {first double quotes and then
single quotes} and it worked. Specifically, this works: VALUES
('100', '" @.variable "'), though you can't tell the order in that text.
I'm using QA.
Thank you everyone for your incredibly quick responses.
-tom|||tom wrote:
>
> Is it possible that adding double quotes and single quotes changed the
> outcome? I adding quotes thus: " ' {first double quotes and then
> single quotes} and it worked. Specifically, this works: VALUES
> ('100', '" @.variable "'), though you can't tell the order in that
> text. I'm using QA.
>
Did you run my script? Why are you messing around with double quotes? Are
you trying to create a dynamic sql statement? If so, see
http://www.sommarskog.se/dynamic_sql.html
My suggestion would be to not use dynamic sql.
You are prolonging this process by failing to give us enough information.
Show us the code needed to cause the error you are getting. Oh yeah! And
tell us what error you are getting ...
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.|||> Show us the code needed to cause the error you are getting. Oh yeah! And
>tell us what error you are getting ...
Here it is . ..
set nocount on
declare fix_cursor cursor
for select ID from mock
declare @.thiskey char(10)
open fix_cursor
fetch next from fix_cursor into @.thiskey
while @.@.FETCH_STATUS = 0
begin
insert into tasks_full (taskid, taskactkey)
values ('" @.thiskey "', (select max(actkey) from actions_full where
actid = '"@.thiskey"'))
fetch next from fix_cursor into @.thiskey
end
close fix_cursor
deallocate fix_cursor
For this statement I get the following error:
Server: Msg 170, Level 15, State 1, Line 15
Line 15: Incorrect syntax near '" @.thiskey "'.
Server: Msg 170, Level 15, State 1, Line 15
Line 15: Incorrect syntax near ')'.
-tom

No comments:

Post a Comment