.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
satyapriyanayak
Sambanthamoorthy
Post New Web Links

INSERT statement with OUTPUT clause, referencing outer query columns

Posted By:      Posted Date: August 30, 2010    Points: 0   Category :Sql Server
 
I have a problem to solve and I have run into what appears to be a limitation of TSQL.  I have looked around and did not find much on this subject, so I apologize if this duplicates another post.  I am using SQL Server 2008. From what I have read on BOL, when you are performing a DELETE or UPDATE statement, you can reference unaffected columns from the outer query in the OUTPUT clause, but this is apparently not allowed in an INSERT statement. I am working on a process that will create new copies of existing records - essentially, the user can create a whole new copy of a set of records, and the process requires that I track both what the original PK values were and the corresponding PK values for the new rows. This example will hopefully spell out my problem.  This script shows two tables, [Primary_Object] and [Secondary_Object].  Not shown here are multiple tables that rely on [Secondary_Table], which is why I have to be able to track this info. This first script shows the setup of the tables involved and the data involved: /* create test data */ create table primary_object ( primary_object_id int identity(1,1), parent_object_id int, name char(1)) create table secondary_object ( secondary_object_id int identity, primary_object_id int, amount money) insert into primary_object (parent_object_id, name) select 0, 'A' insert into secon


View Complete Post


More Related Resource Links

Query help to write case statement in Where clause?

  
Hi All, I have table something like this:- SELECT UserName, IsManager FROM Employee IsManager has only two values either 1 (which means user is a Manager) or 0 (which means user is not a Manager) Now I want to write below query:- DECLARE @IsManager INT SET @IsManager = 2 SELECT UserName, IsManager FROM Employee WHERE IsManager = CASE WHEN @IsManager = 1 THEN 1 WHEN @IsManager = 0 THEN 0 WHEN @IsManager = 2 THEN 1 | 0 ----- THIS IS NOT WORKING, when I set @IsManager value to 2 then where clause should be like this "IsManager = 1 OR IsManager = 0" END Can anybody help me out in modifying about query so that it produces both the results when @IsManager variable value "2". Thanks Regards, Kumar

Two Output Columns from One CASE Statement

  
I have some query logic that needs to return two dynamically created columns based on two conditional tests so that - If Test1 is True then the values for columns 1 and 2 are always the same regardless of the results of Test2. - If Test1 is False then Test2 determines the value of Column2 Because the CASE statement only produces one column I am having to repeat Test1 and then nest Test2 in the ELSE portion of the second iteration of Test1.  I was hoping that someone might have a different approach that would reduce the amount of testing being performed in the SQL.  I did see the post at http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a0e45c7d-eedd-41ca-89f9-163c4dadc1a7/ that doesn't offer much hope but I thought I'd ask. For what it's worth I'm posting the heart of my logic below.  The test is performing bitwise comparisons to determine (Test1:) If the currently logged in user has access to a document and, if not (Test2) what they need to do to gain access.  @Access will end up as a parameter in the stored procedure once I finalize this. DECLARE   @Access tinyint = 1 SELECT   Holding.HoldingID, ShortTitle, Access,   CASE     WHEN @Access &[Access] = @Access THEN (URL + [FileName])     ELSE null   END AS [HoldingURL],         CASE

Using CASE statement in "IN" clause of query

  

I have a sql datasource with the ControlParameter called ddlDropDownList.

2 questions:

1. Is my syntax below ok, I am getting a runttime error that the syntax is incorrect.

2. I have the select statement set up the control parameter to get the selected value from the dropdownlist as shown below, is that syntax correct?  especially the "selectedItem.value" for the propertyname property. thx


select fieldOne, fieldTwo from someTable where fieldOne IN 
CASE WHEN @ddlDropDownList='valueABC' THEN ('ABC') WHEN @ddlDropDownList='valueDEF' THEN ('DEF') 
ELSE ('ABC','DEF') END 

<asp:ControlParameter ControlID="ddlDropDownList" Name="typeSlctd" PropertyName="SelectedItem.Value" Type="String" />



Access Services - The query has too many output columns.

  

Trying to port a database from Access (2000 or older! via an intermediate upgrade to 2010) to use Access Services on Sharepoint 2010.  The main data entry table contains 41 columns of data and I'm getting this error when trying to load it as a web form: "The query has too many output columns. The maximum number of output columns is 40."

I take it the database needs re-designing to split some of the data off into separate tables to be accessed on-demand?  Or is there a setting inside Sharepoint I can tweak to get the form to display?


Linq Query with dynamic where clause i.e filter with dynamic no of filter columns

  

Hi,

I want a linq query with dynamic where clause.

Following is explanation of problem:

e.g. 

Following line returns me n no of rows.

IEnumerable<XElement>  xeleRows = (from xele in xdocFile.Root.Elements(strCtlLookUpXMLNode) select xele);

structure of xeleRows will be like:

<A1>

<Ele1>Val1</Ele1>

<Ele1>Val2</Ele1>

</A1>

.

.

 

<An>

<Elen>Valn</Elen>

<Elen>Valn</Elen>

</An>

 

Now I want to get one row from N Number of rows on basis of filter criteria that can be dynamic, the no of columns in filter criteria will vary at run time.

Anybody know shows to achieve this using Linq. Please guide.

Thanks.

 


MS SQL Server: Search All Tables, Columns & Rows For Data or Keyword Query

  
If you need to search your entire database for specific data, this query will come in handy.

So when a client needs a custom report or some sort of custom development using Great Plains, most of the time I will have to track down the data in the system by running this query and find the table(s) it is in.

SQL Query Where Clause

  

Hello,

FROM dbo.SSD a , dbo.SSAAppEAL b
WHERE a.SSN = b.SSN
AND (a.AssocClaim = 1)
AND b.somedate = SELECT MAX (b1.somedate)
                   FROM dbo.SSAAppEAL b1
                   WHERE b1.SSN = b.SSN
                   AND b1.somedate < sysdate


When ever I add this Query to my Select Statement. SQL management studio is shutting down abruptly.

The mail fault is with this statement

b.somedate = SELECT MAX (b1.somedate)
                   FROM dbo.SSAAppEAL b1
                   WHERE b1.SSN = b.SSN
                   AND b1.somedate < sysdate


Can any one help me out

TIA


Remove the lines from the query output

  
On TSQL Command of; set nocount onselect @@version Getting the output as;                                                                                                                                                                                                                                                                  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Microsoft SQL Server  2000 - 8.00.2039 (Intel X86)  May  3 2005 23:18:38  Copyright (c) 1988-2003 Microsoft Corporation Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2) How to remove the lines from the output?

INSERT Statement Inside a UDF?

  
I am using SQL Server 2005.  I know you can not put INSERT or UPDATE statements in a UDF.  I am looking for an alternative to getting the end result I need since this call has to go at a specific point in the code or it will never work.  I have a UDF which packs smaller boxes into a bigger box efficiently.  The UDF typically runs and creates as many bigger boxes as possible until all smaller boxes are used and then it returns a TABLE variable back to a stored procedure.  I need to insert a record into a new table at the point where each bigger box is full before it moves on to the next bigger box.  There is only a couple alternatives I can see but don't really know if each is possible.  I am open to an ideas: 1. Call a stored procedure from the UDF which executes the insert statement 2. Using EXECUTE to perform the insert statement 3. Re-write the UDF as a stored procedure.  It must be able to table two inputs and return a table which can be unioned with two other views. 4. Anything Else UPDATE: Scratch idea #1 since it says I can only call other functions or extended stored procedures UPDATE: Scratch idea #2 as it did not work

how to prepare WHERE clause in vertical columns data

  
here the sample table X with datatype againt each data..how to prepare below WHERE clause from table X, WHERE   S_NAME = 'AYAZ' AND S_ROLL = 10 AND S_MARKS = 99 AND S_SCHOOL = 'ZINQ'     and table X is, CREATE TABLE X (UID INT IDENTITY(1,1), COL_N VARCHAR(50), COL_V VARCHAR(50), COL_D VARCHAR(50)) INSERT INTO X VALUES ('S_NAME', 'AYAZ', 'VARCHAR(50)') INSERT INTO X VALUES ('S_ROLL', '10', 'DECIMAL(15, 0)') INSERT INTO X VALUES ('S_MARKS', '99', 'INT') INSERT INTO X VALUES ('S_SCHOOL', 'ZINQ', 'VARCHAR(100)') --SELECT * FROM X  

How to reference 2 columns in a query

  
Hi All I have 4 tables First Table Structure Id           AccountNum 1                Meals 2                Overtime 3                Wage SSAmount Id     SiteNbr     Meals            Overtime           Wage 1       145              100                 500                   1000 GGAmount Id     SiteNbr     Meals            Overtime           Wage 1       145              100                

Can we use Select clause in Case statement

  
SELECT CASE OT.ItemTypeID WHEN 6 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID IN(6,11,12) WHEN 7 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID IN(7,14,15) WHEN 8 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID = 8 WHEN 9 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID = 9 WHEN 10 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID = 10 WHEN 11 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID IN(6,11,12) WHEN 12 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID IN(6,11,12) WHEN 14 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID IN(7,14,15) WHEN 15 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID IN(7,14,15) WHEN 18 THEN SELECT SUM(ISNULL("Item Cost",0)) FROM SUBTABLE IT WHERE IT.OrderID = OT.OrderID AND IT.ItemTypeID = 18 WHEN 19 THEN SELECT SUM(ISNULL(&qu

SQL Query keeps failing on Where clause

  
I am very new to SQL so forgive me for this. We are upgrading to Dynamics AX 2009 and in doing so I have been volunteered as the report writer. Lucky me. I have a query that should return all items for a customer and their resective OnHand quantity in one field and in another field it should only return an OnHand quantity if certain conditions are met in another field, in this case a location field (example; product in Embargo). Also this Embargo could be a variation of locations, (example) louembarg, embargff, louembargun, etc. I have cleaned my query back up since I believe my path was way off track. Below the query is an example of what the end result should be. SELECT     TOP (100) PERCENT dbo.INVENTTABLE.DATAAREAID, dbo.INVENTTABLE.CUSTACCOUNT, dbo.INVENTTABLE.CUSTDIVISIONID,                       dbo.INVENTTABLE.PACKAGINGGROUPID, dbo.INVENTTABLE.ITEMGROUPID, dbo.INVENTTABLE.ITEMID, dbo.INVENTTABLE.NAMEALIAS,                       dbo.INVENTTABLE.ITEMNAME, dbo.INVENTTABLE.STANDARDPALLETQUANTITY, dbo.INVENTTABLE.BOMUNITID, dbo.INVENTDIM.WMSLOCATIONID,            &n

Get output from storedprocedure and assign in Select statement

  
Is it possible to get the output from stored procedure and assign into select statement. for example. Here prc_GetComplete is another storeprocedure to get the value for @Completes INSERT INTO TABELE1 (ID, address, completes ) SELECT NEWID(), t2.address, @Completes = EXEC prc_GetComplete t2.ID , @Completes OUT FROM TABELE2 t2   Regards, teesh
Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend