Wednesday, March 28, 2012
INSERT...SELECT and OUTPUT question
begin tran
USE AdventureWorks
GO
DECLARE @.MyTableVar table (
ProductID int NOT NULL,
ProductName nvarchar(50)NOT NULL,
ProductModelID int NOT NULL,
PhotoID int NOT NULL);
DELETE Production.ProductProductPhoto
OUTPUT DELETED.ProductID,
p.Name,
p.ProductModelID,
DELETED.ProductPhotoID
INTO @.MyTableVar
--OUTPUT DELETED.ProductID, DELETED.ProductPhotoID, GETDATE() AS DeletedDate
FROM Production.ProductProductPhoto AS ph
JOIN Production.Product as p
ON ph.ProductID = p.ProductID
WHERE p.ProductID BETWEEN 800 and 810;
--Display the results of the table variable.
SELECT ProductID, ProductName, PhotoID, ProductModelID
FROM @.MyTableVar;
GO
rollback
This is not valid. Why not? Am I missing something?
Is it that only with UPDATE/DELETE other fields from JOIN can be in output ?
USE AdventureWorks ;
GO
IF OBJECT_ID ('dbo.EmployeeSales', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeSales;
GO
CREATE TABLE dbo.EmployeeSales
( EmployeeID nvarchar(11) NOT NULL,
LastName nvarchar(20) NOT NULL,
FirstName nvarchar(20) NOT NULL,
CurrentSales money NOT NULL,
ProjectedSales money NOT NULL
);
GO
INSERT INTO dbo.EmployeeSales
OUTPUT INSERTED.EmployeeID,
INSERTED.LastName,
INSERTED.FirstName,
INSERTED.CurrentSales,
e.EmployeeID
SELECT e.EmployeeID, c.LastName, c.FirstName, sp.SalesYTD, sp.SalesYTD * 1.1
0
FROM HumanResources.Employee AS e
INNER JOIN Sales.SalesPerson AS sp
ON e.EmployeeID = sp.SalesPersonID
INNER JOIN Person.Contact AS c
ON e.ContactID = c.ContactID
WHERE e.EmployeeID LIKE '2%'
ORDER BY c.LastName, c.FirstName;
GO
SELECT EmployeeID, LastName, FirstName, CurrentSales, ProjectedSales
FROM dbo.EmployeeSales;
GOFarmer (someone@.somewhere.com) writes:
> Why would this syntax be valid and the second one is not (below)?
> DELETE Production.ProductProductPhoto
> OUTPUT DELETED.ProductID,
> p.Name,
> p.ProductModelID,
> DELETED.ProductPhotoID
> INTO @.MyTableVar
> FROM Production.ProductProductPhoto AS ph
> JOIN Production.Product as p ON ph.ProductID = p.ProductID
> WHERE p.ProductID BETWEEN 800 and 810;
>...
> INSERT INTO dbo.EmployeeSales
> OUTPUT INSERTED.EmployeeID,
> INSERTED.LastName,
> INSERTED.FirstName,
> INSERTED.CurrentSales,
> e.EmployeeID
> SELECT e.EmployeeID, c.LastName, c.FirstName, sp.SalesYTD,
> sp.SalesYTD * 1.10
> FROM HumanResources.Employee AS e
>...
The syntax diagram in Books Online gives us:
<column_name> ::=
{ DELETED | INSERTED | from_table_name } . { * | column_name }
In the comments section we find:
from_table_name
Is a column prefix that specifies a table included in the FROM clause
of a DELETE or UPDATE statement that is used to specify the rows to
update or delete.
Thus, Books Online clearly says that you cannot use e.EmployeeID in the
OUTPUT clause of an INSERT statement.
Then remains the question why it is so. We look at the syntax diagram
for INSERT:
[ WITH <common_table_expression> [ ,...n ] ]
INSERT
[ TOP ( expression ) [ PERCENT ] ]
[ INTO]
{ <object> | rowset_function_limited
[ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
}
{
[ ( column_list ) ]
[ <OUTPUT Clause> ]
{ VALUES ( { DEFAULT | NULL | expression } [ ,...n ] )
| derived_table
| execute_statement
}
}
| DEFAULT VALUES
[; ]
Note here that the SELECT statement appears in this grammar as a
derived table. A derived table has the property, that it does not
see things outside of if, and the outside cannot look in.
In looser terms, we can simply say that the OUTPUT clause is part of
the INSERT clause in a way that the SELECT statement is not, and thus
does not have visibility of what is in the SELECT statement.
For DELETE or UPDATE it's a different matter as the FROM clause are
part of the DELETE and UPDATE statments themselves.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thank you,
very good explanation on your part. I see it now. I should have read more
carefully.
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns976FD66F91C3BYazorman@.127.0.0.1...
> Farmer (someone@.somewhere.com) writes:
> The syntax diagram in Books Online gives us:
> <column_name> ::=
> { DELETED | INSERTED | from_table_name } . { * | column_name }
> In the comments section we find:
> from_table_name
> Is a column prefix that specifies a table included in the FROM clause
> of a DELETE or UPDATE statement that is used to specify the rows to
> update or delete.
> Thus, Books Online clearly says that you cannot use e.EmployeeID in the
> OUTPUT clause of an INSERT statement.
> Then remains the question why it is so. We look at the syntax diagram
> for INSERT:
> [ WITH <common_table_expression> [ ,...n ] ]
> INSERT
> [ TOP ( expression ) [ PERCENT ] ]
> [ INTO]
> { <object> | rowset_function_limited
> [ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
> }
> {
> [ ( column_list ) ]
> [ <OUTPUT Clause> ]
> { VALUES ( { DEFAULT | NULL | expression } [ ,...n ] )
> | derived_table
> | execute_statement
> }
> }
> | DEFAULT VALUES
> [; ]
> Note here that the SELECT statement appears in this grammar as a
> derived table. A derived table has the property, that it does not
> see things outside of if, and the outside cannot look in.
> In looser terms, we can simply say that the OUTPUT clause is part of
> the INSERT clause in a way that the SELECT statement is not, and thus
> does not have visibility of what is in the SELECT statement.
> For DELETE or UPDATE it's a different matter as the FROM clause are
> part of the DELETE and UPDATE statments themselves.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||Farmer, try this one
IF OBJECT_ID ('dbo.EmployeeSales', 'U') IS NOT NULL
DROP TABLE dbo.EmployeeSales;
GO
CREATE TABLE dbo.EmployeeSales
( EmployeeID nvarchar(11) NOT NULL,
LastName nvarchar(20) NOT NULL
);
GO
CREATE TABLE #Temp ( EmployeeID int not null,
LastName nvarchar(20) NOT NULL)-
INSERT INTO dbo.EmployeeSales(EmployeeID,LastName)
OUTPUT INSERTED.EmployeeID, INSERTED.LastName INTO #Temp
SELECT e.EmployeeID, c.LastName
FROM HumanResources.Employee AS e
INNER JOIN Sales.SalesPerson AS sp
ON e.EmployeeID = sp.SalesPersonID
INNER JOIN Person.Contact AS c
ON e.ContactID = c.ContactID
WHERE e.EmployeeID LIKE '2%'
ORDER BY c.LastName, c.FirstName;
select * from #Temp
go
"Farmer" <someone@.somewhere.com> wrote in message
news:%23AWUfJbNGHA.3164@.TK2MSFTNGP11.phx.gbl...
> Thank you,
> very good explanation on your part. I see it now. I should have read more
> carefully.
> "Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
> news:Xns976FD66F91C3BYazorman@.127.0.0.1...
>|||Thanks
You have missed my point though. This does not work and this can be a field
from a JOIN table from FROM statement.
OUTPUT e.EmployeeID, INSERTED.LastName INTO #Temp
"Uri Dimant" <urid@.iscar.co.il> wrote in message news:O$1jqNeNGHA.3936@.TK2MSFTNGP12.phx.gbl
..
> Farmer, try this one
>
> IF OBJECT_ID ('dbo.EmployeeSales', 'U') IS NOT NULL
>
> DROP TABLE dbo.EmployeeSales;
>
> GO
>
> CREATE TABLE dbo.EmployeeSales
>
> ( EmployeeID nvarchar(11) NOT NULL,
>
> LastName nvarchar(20) NOT NULL
>
> );
>
> GO
>
> CREATE TABLE #Temp ( EmployeeID int not null,
>
> LastName nvarchar(20) NOT NULL)-
>
> INSERT INTO dbo.EmployeeSales(EmployeeID,LastName)
>
> OUTPUT INSERTED.EmployeeID, INSERTED.LastName INTO #Temp
>
> SELECT e.EmployeeID, c.LastName
>
> FROM HumanResources.Employee AS e
>
> INNER JOIN Sales.SalesPerson AS sp
>
> ON e.EmployeeID = sp.SalesPersonID
>
> INNER JOIN Person.Contact AS c
>
> ON e.ContactID = c.ContactID
>
> WHERE e.EmployeeID LIKE '2%'
>
> ORDER BY c.LastName, c.FirstName;
>
>
>
> select * from #Temp
>
> go
>
> "Farmer" <someone@.somewhere.com> wrote in message
> news:%23AWUfJbNGHA.3164@.TK2MSFTNGP11.phx.gbl...
>
>
Monday, March 19, 2012
Insert Syntax Error
Server: Msg 170, Level 15, State 1, Line 17
Line 17: Incorrect syntax near ')'.
When I try to execute this code from SQL Query Analyzer...
DECLARE @.DPPNumberCursor INT
DECLARE DPPNumberCursor Cursor for Select PPAP_ID
from ppap
where ppap_cancel <> "1" and
ppap_close <> "1" and
projectonhold <> "1"
OPEN DPPNumberCursor
Fetch Next From DPPNumberCursor
INTO @.dppnumbercursor
While @.@.Fetch_Status = 0
Begin
INSERT INTO APQPSubformTable (apqpsub_id)
Values (@.dppnumbercursor)
Bsically, I want to insert the number held in @.dppnumbercursor in the APQPSub_id field.
Any help would be appreciated.How about END at the end?|||...and DEALLOCATE/CLOSE would be very appropriate ;)|||OK, I'm an idiot...can you tell I'm a newbie? Thanks for not tearing me up too bad :-)
Thanks to both for your help....|||You're welcome...from both of us ;)|||Nobody is going to mention the double quotes?
Or the fact that s/he doesn't need a cursor at all?
INSERT INTO APQPSubformTable(apqpsub_id)
SELECT PPAP_ID
FROM ppap
WHERE ppap_cancel <> '1'
AND ppap_close <> '1'
AND projectonhold <> '1'|||I was thinking about it, but then decided not to...when he/she thanked "both" of us ;)|||It's been one of those days:-)
Just for my own edification, what about the double quotes?
The reason I used the cursor was because I needed to add two records into the APQPSubformtable for each open project in the ppap table. Here is the completed working code...
DECLARE @.DPPNumberCursor INT
DECLARE DPPNumberCursor Cursor for Select PPAP_ID
from ppap
where ppap_cancel <> "1" and
ppap_close <> "1" and
projectonhold <> "1"
OPEN DPPNumberCursor
Fetch Next From DPPNumberCursor
INTO @.dppnumbercursor
While @.@.Fetch_Status = 0
Begin
INSERT INTO APQPSubformTable (apqpsub_id,[Key_element#],key_element, resp_area, [Fkey_element#], GYR_status)
Values (@.dppnumbercursor,"31","Start Of Production (SOP) [F4]","OP","0","G")
INSERT INTO APQPSubformTable (apqpsub_id,[Key_element#],key_element, resp_area, [Fkey_element#], GYR_status)
Values (@.dppnumbercursor,"32","Stable Production (Review) [F5]","OP","0","G")
FETCH NEXT from DPPNumberCursor
INTO @.DPPNumberCursor
End
Close DPPNumberCursor|||Double-quotes are harmless as long as you know what your connection settings are when you compile your procedure/function/trigger/view (should be SET QUOTED_IDENTIFIER OFF).|||Just for my own edification, what about the double quotes?
It is a better practice to use single quotes to delimit strings. Double quotes can serve two purposes: delimit a string and delimit an identifyer (ie table name or column name). There is a QUOTED_IDENTIFYER property the determines which. Set to ON, everything within double quotes is supposed to be considered an Identifyer.
I've read in BOL that SLQ Server is 'not very rigorous' in enforcing this rule, depending on the length of the string in question. Small strings such as the ones you are using seem to work OK, but you'd be better off delimiting the strings correctly than relying on SQL Analyzer to interpret what it is you really mean.|||Don't Play with Settings...
INSERT INTO APQPSubformTable (apqpsub_id,[Key_element#],key_element, resp_area, [Fkey_element#], GYR_status)
SELECT @.dppnumbercursor,'31','Start Of Production (SOP) [F4]','OP','0','G'
UNION ALL
SELECT @.dppnumbercursor,'32','Stable Production (Review) [F5]','OP','0','G'
Insert syntax error
"INSERT INTO tblLocking (Table, Record, User) VALUES ('tblDiscrepancy','70','mnewheiser')"
I am getting a syntax error on the above statement. All fields that i am inserting to (Table, Record and User) are all text types, and the table does exist.
There is a fourth column in the table (LockID) which is a Access [Auto-Number] type(why i'm not sure on the forum) do i need to declare this in the statement.
Any help would be much appreciated as its is beginning to drive me mad.
CheersOk, so it appears the solutions wasn't as complicated as i thought it would be.
Although 'Table' and 'User' where valid columns in the table, they are also reserved words (or something like that). So in the statement they needs [ and ] around them (i.e. [Table])
Don't have to slit my wrists now. :)
Insert Stored Procedure Help
OK I have a stored procedure that inserts information into a database table. Here is what I have so far:
I think I have the proper syntax for inserting everything, but I am having problems with two colums. I have Active column which has the bit data type and a Notify column which is also a bit datatype. If I run the procedure as it stands it will insert all the information correctly, but I have to manually go in to change the bit columns. I tried using the set command, but it will give me a xyntax error implicating the "=" in the Active = 1 How can I set these in the stored procedure?
1SET ANSI_NULLSON2GO3SET QUOTED_IDENTIFIERON4GO5-- =============================================6-- Author:xxxxxxxx7-- Create date: 10/31/078-- Description:Insert information into Registration table9-- =============================================10ALTER PROCEDURE [dbo].[InsertRegistration]1112@.Name nvarchar(50),13@.StreetAddressnchar(20),14@.Citynchar(10),15@.Statenchar(10),16@.ZipCodetinyint,17@.PhoneNumbernchar(20),18@.DateOfBirthsmalldatetime,19@.EmailAddressnchar(20),20@.Gendernchar(10),21@.Notifybit2223AS24BEGIN25-- SET NOCOUNT ON added to prevent extra result sets from26-- interfering with SELECT statements.27SET NOCOUNT ON;2829INSERT INTO Registration3031(Name, StreetAddress, City, State, ZipCode, PhoneNumber, DateOfBirth, EmailAddress, Gender, Notify)3233VALUES3435(@.Name, @.StreetAddress, @.City, @.State, @.ZipCode, @.PhoneNumber, @.DateOfBirth, @.EmailAddress, @.Gender, @.Notify)3637--SET38--Active = 13940END41GO
Can u post the data with execute method
Thank u
Baba
|||You cannot set "Active" = 1 in t-sql.
That is because "Active" is not a valid variable name. It should be @.Active and, of course, you would have to declare it before doing so.
That said, I suspect you are trying to set the Active column value to 1 in the record you are inserting.
If so, you need to add (assuming Active is the column name) Active to the list of column names in the insert statement.
In the corresponding slot in the list of values in the insert statement, place a 1.
|||It didn't correct the problem, but let me make sure I am on the right track (different table here):
1set ANSI_NULLSON2set QUOTED_IDENTIFIERON3GO4-- =============================================5-- Author:xxxxx
6-- Create date: 10/21/077-- Description:Insert Users8-- =============================================9ALTER PROCEDURE [dbo].[InsertUser]101112@.FirstNamenvarchar(50),13@.LastNamenvarchar(50),14@.MiddleNamenvarchar(50),15@.Activebit16AS17BEGIN18SET NOCOUNT ON;1920INSERT INTO Users21(FirstName, LastName, MiddleName, Active)22VALUES23(@.FirstName, @.LastName, @.MiddleName, 1)24--SET25--Active =126END
I get this message when you try to insert a user:
Procedure or function 'InsertUser' expects parameter '@.Active', which was not supplied.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:System.Data.SqlClient.SqlException: Procedure or function 'InsertUser' expects parameter '@.Active', which was not supplied.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.Stack Trace:
[SqlException (0x80131904): Procedure or function 'InsertUser' expects parameter '@.Active', which was not supplied.] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +921162 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +800038 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +186 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1932 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +947 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +149 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +404 System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values) +447 System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +72 System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +390 System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +602 System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +95 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35 System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +109 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +115 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +132 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +177 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746
Tharnid:
12 @.FirstNamenvarchar(50),
13 @.LastNamenvarchar(50),
14 @.MiddleNamenvarchar(50),
15 @.Activebit
Tharnid:
Procedure or function 'InsertUser' expects parameter '@.Active', which was not supplied.
You declared an input parameter @.Active, according to your SQL statement this should be 1 by default, so you should not declare it at all!
12 @.FirstNamenvarchar(50),
13 @.LastNamenvarchar(50),
14 @.MiddleNamenvarchar(50)
15
try:
ALTER PROCEDURE [dbo].[InsertUser]
10
11
12@.FirstNamenvarchar(50),
13@.LastNamenvarchar(50),
14@.MiddleNamenvarchar(50),
15@.Activebit = 1
16AS
BEGIN
...
END
|||
Procedure or function 'InsertUser' expects parameter '@.Active', which was not supplied.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details:System.Data.SqlClient.SqlException: Procedure or function 'InsertUser' expects parameter '@.Active', which was not supplied.
Simply is telling you that you are not supplying the code with the value for the Active Parameter which in your case is an INPUT parameter and not an OUTPUT parameter. If you need it to be OUTPUT, I believe you have to indicate that in your procedure.
|||
Yes, you can give @.Active a default value as Spark is suggesting, but why would you do that? In the SQL statement active is hardcoded to be 1. So if you do supply a value (0 or 1) for it, the procedure will ignore it!
And yes, you can make it an output parameter as Dollarjunkie wrote, but then the return statement should be
SET @.active = 1
But why would you want an output parameter that will always return 1?
In this case, to skip the parameter is the logical solution, although the other 2 approaches will also work...
|||I appreciate the respone :-) I will try them as soon as I can, indicate who gave the correct answer, and check answered
Friday, March 9, 2012
Insert row in Query Analyzer
I have a table called customer which got
id = int
name = varchar
address = varchar
email= varchar
can you please write the syntax that insert the below data in the table using query Analyzer
id = 1
name = fadil
Address = London
email = fadil1977@.hotmail.com
Thank you for your time and help.INSERT INTO Customer
VALUES (1, 'fadil', 'London', 'fadil1977@.hotmail.com')
Wednesday, March 7, 2012
insert question
This is more of a sql query syntax question then anything but I need to run it on my handheld.
I have 3 data collection tables, and 3 staging tables that will be used to PUSH the data back to the SQL Server.
so here is what happens:
to populate staging table 1 I do
insert into stagingtable1 (id, col1, col2, col3) select newid, col1, col2, col3 from datacollection table1
staging table 2:
insert into stagingtable2 (id) select id from stagingtable1
staging table 3.
insert into stagingtable2 (id - (now this ID field needs to be the same id field as staging table 2 and staging table 1 have) col1, col2, col3, col4, from DataCollectionTable2
how can I create the insert SQL for my staging table 3?
revised:
there is no columns I can join on to get the uploadID to insert into stagingtable 3
INSERT QUERY!
I am attempting to populate a table with the results of the following
queries.
Unsure of what syntax is required.
Help appreciated!!!!
INSERT INTO [Question] (ExamID, Question, Idx) ?
(SELECT MAX(ExamID) FROM [EasyLearning].[dbo].[Exam]),
(SELECT ex2_mult_Q FROM exam2 WHERE ex2_name = @.ExamName),
(SELECT Idx FROM Exam WHERE ExamID = (SELECT MAX(ExamID) FROM
[EasyLearning].[dbo].[Exam]) + 1)
Cheers AdamHi Adam,
INSERT INTO [Question] (ExamID, Question, Idx)
SELECT
(SELECT MAX(ExamID) FROM [EasyLearning].[dbo].[Exam]) AS ExamID,
(SELECT ex2_mult_Q FROM exam2 WHERE ex2_name = @.ExamName) AS
Question,
(SELECT Idx FROM Exam WHERE ExamID =
(
SELECT MAX(ExamID) FROM [EasyLearning].[dbo].[Exam]) + 1
)
) AS Idx
HTH, jens Suessmeyer.|||Look up INSERT.SELECT in Books Online, but start by building the SELECT quer
y
that returns all the values that you intend to insert.
Once the query returns the correct values, use it in the INSERT...SELECT
query.
If more than one row should be returned, make sure the values are properly
related - right now I see no relationship between the three selects - but if
you do, and it works as you expect, then that's good enough for me.
ML
http://milambda.blogspot.com/
Sunday, February 19, 2012
Insert other db problem
I get incorrect syntax ner '100' on this one :(
INSERT INTO 100Test.myuser.Test
(Something)
SELECT Something FROM TestDB
why?
TIA
/LasseTry this:
INSERT INTO [100Test].myuser.Test
(Something)
SELECT Something FROM TestDB
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Lasse Edsvik" <lasse@.nospam.com> wrote in message
news:uXVjK$HMFHA.2132@.TK2MSFTNGP14.phx.gbl...
Hello
I get incorrect syntax ner '100' on this one :(
INSERT INTO 100Test.myuser.Test
(Something)
SELECT Something FROM TestDB
why?
TIA
/Lasse|||Look for "Rules for Regular Identifiers" in BOL.
AMB
"Lasse Edsvik" wrote:
> Hello
> I get incorrect syntax ner '100' on this one :(
> INSERT INTO 100Test.myuser.Test
> (Something)
> SELECT Something FROM TestDB
> why?
> TIA
> /Lasse
>
>|||Try:
INSERT INTO [100Test].myuser.Test
Use [] if the database name starts with a number
"Lasse Edsvik" wrote:
> Hello
> I get incorrect syntax ner '100' on this one :(
> INSERT INTO 100Test.myuser.Test
> (Something)
> SELECT Something FROM TestDB
> why?
> TIA
> /Lasse
>
>|||Invalid objectname '100Test.myuser.Test'
the db and table is there on same server..... and user has rights to that
table...... sigh..... not sure how such a simple thing can go wrong....
"Narayana Vyas Kondreddi" <answer_me@.hotmail.com> wrote in message
news:%2309qXCIMFHA.3548@.TK2MSFTNGP10.phx.gbl...
> Try this:
> INSERT INTO [100Test].myuser.Test
> (Something)
> SELECT Something FROM TestDB
>
> --
> HTH,
> Vyas, MVP (SQL Server)
> SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
>
> "Lasse Edsvik" <lasse@.nospam.com> wrote in message
> news:uXVjK$HMFHA.2132@.TK2MSFTNGP14.phx.gbl...
> Hello
> I get incorrect syntax ner '100' on this one :(
> INSERT INTO 100Test.myuser.Test
> (Something)
> SELECT Something FROM TestDB
> why?
> TIA
> /Lasse
>
>|||The rule of identifiers state that the first character of an object name
should be a letter, underscore (_), "at" sign (@.), or number sign (#). If
any other characters occur, then you will have to use it as a delimited
identifier as [100Test] or "100Test".
Anith|||Now it "works".....
rows get inserted but i get this error:
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionCheckForData
(CheckforData()).
Server: Msg 11, Level 16, State 1, Line 0
General network error. Check your network documentation.
Connection Broken
hmm?
"Lasse Edsvik" <lasse@.nospam.com> wrote in message
news:uXVjK$HMFHA.2132@.TK2MSFTNGP14.phx.gbl...
> Hello
> I get incorrect syntax ner '100' on this one :(
> INSERT INTO 100Test.myuser.Test
> (Something)
> SELECT Something FROM TestDB
> why?
> TIA
> /Lasse
>
INSERT or UPDATE syntax?
In my SQL Server code I have to have large IF blocks in order to replicate
this -- in a trigger or stored proc you don't know in advance which one
you're going to have to do.
Am I missing something here? Is there an easy way to replicate this feature
in SQL Server?
Maury
Nope, it's not there. Send a request for this feature to:
sqlwish@.microsoft.com
"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:DC54237D-E872-45A1-86F2-48F29665F9BD@.microsoft.com...
> Oracle and a few other DB's contain a VERY nice "INSERT OR UPDATE"
feature.
> In my SQL Server code I have to have large IF blocks in order to replicate
> this -- in a trigger or stored proc you don't know in advance which one
> you're going to have to do.
> Am I missing something here? Is there an easy way to replicate this
feature
> in SQL Server?
> Maury
|||There is no UPSERT or MERGE.
http://groups.google.com/groups?hl=e...phx.gbl#link1
http://groups.google.com/groups?hl=e...phx.gbl#link1
http://www.aspfaq.com/
(Reverse address to reply.)
"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:DC54237D-E872-45A1-86F2-48F29665F9BD@.microsoft.com...
> Oracle and a few other DB's contain a VERY nice "INSERT OR UPDATE"
feature.
> In my SQL Server code I have to have large IF blocks in order to replicate
> this -- in a trigger or stored proc you don't know in advance which one
> you're going to have to do.
> Am I missing something here? Is there an easy way to replicate this
feature
> in SQL Server?
> Maury
INSERT or UPDATE syntax?
In my SQL Server code I have to have large IF blocks in order to replicate
this -- in a trigger or stored proc you don't know in advance which one
you're going to have to do.
Am I missing something here? Is there an easy way to replicate this feature
in SQL Server?
MauryNope, it's not there. Send a request for this feature to:
sqlwish@.microsoft.com
"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:DC54237D-E872-45A1-86F2-48F29665F9BD@.microsoft.com...
> Oracle and a few other DB's contain a VERY nice "INSERT OR UPDATE"
feature.
> In my SQL Server code I have to have large IF blocks in order to replicate
> this -- in a trigger or stored proc you don't know in advance which one
> you're going to have to do.
> Am I missing something here? Is there an easy way to replicate this
feature
> in SQL Server?
> Maury|||There is no UPSERT or MERGE.
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&frame=right&th=1bf4ff435f7820e3&seekm=uPfJ7%23jIEHA.2972%40TK2MSFTNGP12.phx.gbl#link1
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&frame=right&th=cd0d91928e55b810&seekm=%23%241tZm0nEHA.3520%40TK2MSFTNGP11.phx.gbl#link1
--
http://www.aspfaq.com/
(Reverse address to reply.)
"Maury Markowitz" <MauryMarkowitz@.discussions.microsoft.com> wrote in
message news:DC54237D-E872-45A1-86F2-48F29665F9BD@.microsoft.com...
> Oracle and a few other DB's contain a VERY nice "INSERT OR UPDATE"
feature.
> In my SQL Server code I have to have large IF blocks in order to replicate
> this -- in a trigger or stored proc you don't know in advance which one
> you're going to have to do.
> Am I missing something here? Is there an easy way to replicate this
feature
> in SQL Server?
> Maury