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

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Insert into select order of execution

Posted By:      Posted Date: October 12, 2010    Points: 0   Category :Sql Server

If I have the following query

SELECT Column1 FROM Table2
How is it executed? Is the Select completed and stored in tempdb then inserted into Table1? Or does it select a row/batch then insert it going back and forth as the result set from the select statement is built?

View Complete Post

More Related Resource Links

How do I INSERT when I don't know the column order

If I know the order that columns are in (FirstName, LastName, PetName), inserting into a table is easy: INSERT INTO MY_TABLE ("Charlie", "Brown", "Snoopy") But if I only know the column names and not their order, how do I insert?

Insert a "Select User" row in Linq for a dropdown list

I'm new to Linq. I have searched and searched the web for a soluion, and can't find anything. I have a Linq query and I want to insert a row ("Select User") to the top before I pass it to the drop down list. I've been trying to use the Union but to now avail (it keeps telling me that my object doesn't support the Union method). My code, prior to attempting to inser a row, is very simple.public SelectList DropDown_Users()        {            var context = new VivarianDataContext();            var query = from t in context.AspnetUsers                        select new { t.UserId, t.LastName };            list = new SelectList(query.AsEnumerable(), "UserId", "LastName");            return list;        }Now I try to insert a row and I found this on the internet and it seems to say that his solution will work. But it is filled with syntax errors. http://magicode.wordpress.com/2009/08/20/inserting-an-item-in-iqueryable-object-using-union-met

order by sort both select in union



I have DropDownList that Show all groups. in that, I want to show "select one group" as default in DDL. for that i used code below. but it sort all return data and "select one group" will not be the first line. How can i fix it?

Best Regards. Morteza


	SELECT   -1 as id,'select one group' as groupName
	SELECT   id, groupName
	FROM     [group] 
	order by groupNam

Conditional insert: If select return rows, insert rows to table otherwise insert specific row indica

This is what I have

Insert into ReportDetail( Partcipantid, Reportid)  

select distinct ParticipantID , 9 from OpenCredit

      except select ParticipantID, 9 from StoreCredit where Closed = 0

 Issue is that when above select statement returns no row, it seems like no record is

How to use the table which is created with CTE(Common Table Expression) more than one select,insert


I faced one problem while working with Common Table expressions in Sqlserver 2008.

the problem is How to use the table(means temporary table)  which is created  by with  CTE's after that one select statement.

Actaully the table(which is created using with CTE) is automatically deleted, one's after execution of any select,insert,update or create view statement .Right!

But I needed that table second time also. How it is possible.

please help if any one knows..........

Thanks in Advance........


ORDER BY items must appear in the select list if SELECT DISTINCT is specified


Hi, i am using a query to get DISTINCT hotels from my Database table sorted by Price and it returns me same hotel times of record found by Price if i use this Query

SELECT distinct(hotelname), availstatus, hoteladdress, hotelphonenumber, hotelpostalcode, destination, starrating, imageurl, hotelcode, TempSession, HotelType, MinimumNightRate from HproTemp where TempSession = '12-53-37' ORDER BY MinimumNightRate

and i get the error "ORDER BY items must appear in the select list if SELECT DISTINCT is specified" When i remove MinimumNightRate from SELECT LIST.

Can anyone have any idea how may i resolve it..

Binding execution order problem?



I'm developing DataGridTreeViewItem custom control which task is to display some icon and title. This control should be placed in TemplateColumn of wpf DataGrid.

Here is the code:

<wpfToolkit:DataGridTemplateColumn Header="Entire Network" Width="Auto" MinWidth="200">

ORDER BY items must appear in the select list if SELECT DISTINCT is specified


why am i getting the error message when both ORDER BY items appear in the SELECT query?



	CONVERT(VARCHAR(8),BI.DateFrom ,3) as FFromDate,
	CONVERT(VARCHAR(8),B.CreatedDate ,3) as FBookingDate,
	CAST(FLOOR( CAST( B.CreatedDate AS FLOAT ) ) AS DATETIME) as BookingDate,
	(C.Forename + CASE 

Keeping the Line Order in BULK INSERT


When loading 50MB file with

FROM 'C:\data\proddesc.txt' 

I get the beginning of the file shifted to the middle approximately. Otherwise the lines are in order (so the document starts in the middle, progresses to bottom, continues at the top).


Kalman Toth, SQL Server & Business Intelligence Training; SQL 2008 GRAND SLAM

Guid clustered index fast select and slow insert



Don't know if this question is in wrong forum but I didnt find a better place.

I have a table called "CompanyApplications". The first column is a guid and is the PK (not clustered). The other colum is also a guid and is the company identifier. This is a guid because it comes from another table (the "company" table), an old table used for other stuff. Then I have a third and forth column for "ApplicationURL" and "ApplName.

My application will do alot of (since 30 000+ users) "select ApplName, ApplicationURL from CompanyApplications where companyId='<the company guid of the user>' order by ApplName). It will make very few inserts into this table. Only when one is inserting a new application.

I have some questions about this since I know it's not good to have a guid as clustered since it will have SQL Server to always resort the clusted index (because guids are "random"):

1. Is it wrong to have the companyId column as clusted in this example? If so, what is better?

2. I know inserts will be relatively slow when using guid as clustered. But in my case the insert does not need to be fast. Will the slow insert also affect selects from other users. I mean, will select's be slow for other users when the index is resorted becuase one user make an insert?


select top 3 order by problem


Hi all


I want get the last 3 tags that added to the db.


My Tag table schema is:



CREATE TABLE [dbo].[tblTag](

      [TagId] [int] IDENTITY(1,1) NOT NULL,

      [Header] [nvarchar](200) NULL,

      [IsActive] [tinyint] NULL DEFAULT ((0)),

      [Ordered] [int] NOT NULL DEFAULT ((1)),

      [Language] [nvarchar](5) NULL,

      [DateTime] [smalldatetime] NULL,

      [Official] [nvarchar](max) NULL,

      [LastUpdate] [datetime] NULL,

      [Locked] [tinyint] NULL,

      [LastUpdateUser] [nvarchar](256) NULL,

      [NumViews] [bigint] NULL,

      [AllUserModified] [nvarchar](max) NULL,



      [TagId] AS

INSERT the Results of a Stored Procedure with Multiple SELECT Statements into Multiple Tables?

I have a stored procedure (which is overly complex and written by someone else) that I need to take the results of and put them into tables.  The stored procedure uses Dynamic SQL to build and then run 5 SELECT statements based on literally hundreds of variables.  I need to take the results of these SELECT statements and put them into tables in a different database.  Rewriting or re-creating the logic of the stored procedure is NOT AN OPTION as the logic may change over time and maintaining it in multiple places would be nightmarish.  Can anybody help me with this?  I've googled extensively, but have only been able to find examples of using one result set not multiple ones.

How to convert image class object to byte type in order to insert database


I am trying to resize images after uploading and than inserting them into my database.

Dim newMainImage As System.IO.Stream = System.IO.Stream.Null
resizedMainImage.Save(newMainImage, jgpEncoder, myEncoderParameters)
Dim newMain As Image = Image.FromStream(newMainImage)

I was able to resize it however I couldnt find a way to convert image object to byte() in order to

I need something like

Dim ImageContent(newMain.ContentLength - 1) As Byte

newMain.InputStream.Read(ImageContent, 0 , newMain.ContentLength)

I need to convert image object to a file type in order to get its contentlenght and use .inputStream.Read function. Or is there any other way?

SELECT INSERT DELETE UPDATE happening at the same time


Hi All,
I have three table and SELECT DELETE INSERT UPDATE happening at the same time.
Join query is used for SELECT and DELETE.
INSERT and UPDATE no join query.
What are the actions to be taken to avoid deadlock. Will indexing help to resolve the dead lock.
Appreciate somebodys help immediately.



Minimal logging not being used on INSERT INTO ... SELECT FROM

I am trying to perform a big INSERT INTO ... SELECT FROM and I just can’t understand why it isn’t using minimal logging.

My SQL Server version is:

Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64)

       Mar 29 2009 10:11:52

       Copyright (c) 1988-2008 Microsoft Corporation

       Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: ) (VM)

I have tried both the Simple and Bulk Logged recovery models for my database.  Nothing seems to make any difference.  There are no replication publications or subscriptions on the server.

I have reduced my query down to the simplest thing I can think of.  I have tried lots of variations on this query; nothing seems to make any difference.  Here’s the query:


select ,insert,update and delete is not getting recorded in the database audit spec.



I am using SQLSERVER 2008 Standard Edition and i m trying to implement database audit for few database. Following is the script that i used:


SERVER AUDIT [audit_mirror]






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