Here is my query which lists all orders for products supplied by Supplier-3.
A typical Query on the Northwind database i wrote is like this..
Select * FROM [Order Details] WHERE ProductID in
(Select ProductID From Products where SupplierID = 3)
The subquery in Red was used in multiple places in one of my Stored Procedures..
So what i thought was - use a temp table to store the resultset from this subquery, and then use the temp table instead of querying the Products table everywhere..
My Query looked something like this..
Declare @ProductIDs TABLE
(ProductID int)
INSERT INTO @ProductIDs
Select ProductID From Products where SupplierID = 3
Select * FROM [Order Details] WHERE ProductID in
(Select ProductID FROM @ProductIDs)
Well, I expected an increase in performance with the latter approach, but seems my Stored Procedure is taking more time with the second solution..
Would be glad to see ne explanation on this behavior..
Simple example: declare @tTable(col1 int) insert into @tTable(col1) values (1) select * from @tTable
Works perfectly in SQL Server Management Studio and the database connection is OK to as I may generate PP table using complex (or simple) queries without difficulty.
But when trying to get this same result in a PP table I get an error, idem when replacing table variable by a temporary table.
Message: OLE DB or ODBC error. .... The current operation was cancelled because another operation the the transaction failed.
I have three queries that I use to determine what the party type of a person.
They will be either a child, family member or an associated party and I need to go to three different tables to find this out. I set a field "relationship" to child, family member or associated party depending on the query results.
I would like to fill a temp table with the results so I can use it in reporting.
Here is my code:
Code: SELECTvap.partyID, relationship INTO#Relationship SELECTvap.partyID, Relationship = 'Child' FROMVolunteerActivityParty vap JOIN VolunteerActivity va ON va.VolunteerActivityID = vap.VolunteerActivityID JOIN
I am querying a table in oracle, the server connection to the Oracle database is determined by a criteria. Though how can I put the results from the oracle query into a temp table ?
This is the code i'm using for the query:
DECLARE @cmd VARCHAR(500) declare @Year varchar(25) set @Year = '2006'
The first query returns me the results from multiple databases, thesecond does the same thing except it puts the result into a #temptable? Could someone please show me an example of this using the firstquery? The first query uses the @exec_context and I am having achallenge trying to figure out how to make the call from within adifferent context and still insert into a #temp table.DECLARE @exec_context varchar(30)declare @sql nvarchar(4000)DECLARE @DBNAME nvarchar(50)DECLARE companies_cursor CURSOR FORSELECT DBNAMEFROM DBINFOWHERE DBNAME NOT IN ('master', 'tempdb', 'msdb', 'model')ORDER BY DBNAMEOPEN companies_cursorFETCH NEXT FROM companies_cursor INTO @DBNAMEWHILE @@FETCH_STATUS = 0BEGINset @exec_context = @DBNAME + '.dbo.sp_executesql 'set @sql = N'select top 10 * from products'exec @exec_context @sqlFETCH NEXT FROM companies_cursor INTO @DBNAMEENDCLOSE companies_cursorDEALLOCATE companies_cursor-------------------------------------------------------------------------------------CREATE TABLE #Test (field list here)declare @sql nvarchar(4000)DECLARE @DBNAME nvarchar(50)DECLARE companies_cursor CURSOR FORSELECT NAMEFROM sysdatabasesWHERE OBJECT_ID(Name+'.dbo.products') IS NOT NULLORDER BY NAMEOPEN companies_cursorFETCH NEXT FROM companies_cursor INTO @DBNAMEWHILE @@FETCH_STATUS = 0BEGINset @sql = N'select top 10 * from '+@DBNAME+'.dbo.products'INSERT INTO #Testexec (@sql)FETCH NEXT FROM companies_cursor INTO @DBNAMEENDCLOSE companies_cursorDEALLOCATE companies_cursorSELECT * from #TestDROP TABLE #Test
I pulled some examples of using a subquery pivot to build a temp table, but cannot get it to work.
IF OBJECT_ID('tempdb..#Pyr') IS NOT NULL DROP TABLE #Pyr GO SELECT vst_int_id, [4981] AS Primary_Ins, [4978] AS Secondary_Ins,
[code]....
The problems I am having are with the integer data being used to create temp table fields. The bracketed numbers on line 7-10 give me an invalid column name error each. In the 'FOR', I get another error "Incorrect syntax near 'FOR'. Expecting '(', or '.'.". The first integer in the "IN" gives me an "Incorrect syntax near '[4981]'. Expecting '(' or SELECT". I will post the definitions from another effort below.
I have a dynamic sql which uses Pivot and returns "technically" variable no. of columns.
Is there a way to store the dynamic sql's output in to a temp table? I don't want to create a temp table with the structure of the output and limit no. of columns hence changing the SP every time I get new Pivot column!!
I want to create index for hash table (#TEMPJOIN2) to reduce the update query run time. But I am getting "Warning!
The maximum key length is 900 bytes. The index 'R5IDX_TMP' has maximum length of 1013 bytes. For some combination of large values, the insert/update operation will fail". What is the right way to create index on temporary table.
Update query is running(without index) for 6 hours 30 minutes. My aim to reduce the run time by creating index.
And also I am not sure, whether creating index in more columns will create issue or not.
Attached the update query and index query.
CREATE NONCLUSTERED INDEX [R5IDX_TMP] ON #TEMPJOIN2 ( [PART] ASC, [ORG] ASC, [SPLRNAME] ASC, [REPITEM] ASC, [RFQ] ASC,
I think this is a very simple question, however, I don't know the answer. What is the difference between a regular Temp table and a Global Temp table? I need to create a temp table within an sp that all users will use. I want the table recreated each time someone accesses the sp, though, because some of the same info may need to be inserted and I don't want any PK errors.
Hi.A question I have is with regard to the use of views with SQL2000. IfI have a view called "A_view" and used in the following manner;----------------SELECT ...FROM A_ViewWHERE ....UNIONSELECT ....FROM A_ViewWHERE .....-----------------is the view computed twice? Ideally if the view is computationallyexpensive I would rather it was only computed once.Also this would be preferred for data consistency.Is there a way to ensure the view is only computes once?Regards JC.......
Hey all. right now I've been spinning my wheels for almost 2 days on a problem with c# 1.1/sqlserv 2005...
I have a dataset that's filled with data that I'm trying to write to a database, I've ensured that the dataset is fine.
I do a transaction to write the code to the table, an insert command, and an update command.
then commit the transaction
Everything seems to work fine, but when I go look at the table, it doesn't actually write any rows!
The weird thing is deletion of the rows works fine on the same table.
I look at the sqlserver tracer and watch it do the deletion on the table, skip right over the insert statements (ie: no insert statements show up whatsoever in the tracer) then commit the transaction.
If anyone has ever heard of something like this happening i would really appreciate some advice. I'm trying to be as specific as I can about the problem, and I've been spinning my wheels for almost 2 days without avail.
I've tried making other tables and writing to them, getting the same results. I think it's somehow not referencing everything correctly but again, everything seems like it's fine.
I've been handed a SQL Server that is used as an MIS source. There are 4 databases that carry out the task of importing data from various sources, then manipulting that data, and offering the data for reporting purposes.
The vendor has also created several other databases (of which there are also 4), but no-one in my company seems to know the purpose of these dbs.
In the logs, there are approximately 8/9 messages per second - not every second, but numberous seconds per minute - stating....
Starting up database 'db_name'.
... each time, all 4 of the mysterious dbs appear.
I've checked the spid that is running this job this morning, and it seems to be NT AUTHORITYSYSTEM connected to one of the original 4 report databases.
Does this have any affect on the performance of the server, or the specific db attached to the user?
I have a table with 3 columns , let say (PatientID int, AppointmentDate date, PatientName varchar(30))
My source data looks in below way..
PatientID AppointmentDate PatientName 1 01/01/2012 Tom 2 01/10/2012 Sam 3 02/15/2012 John
I need output in below way..
PatientID AppointmentDate PatientName 1 01/01/2012 Tom (actual patient record) null 01/10/2012 Tom null 02/15/2012 Tom null 01/01/2012 Sam 2 01/10/2012 Sam (actual patient record) null 02/15/2012 Sam null 01/01/2012 John null 01/10/2012 John 3 02/15/2012 John (actual patient record)
I need t-sql to get above output. Basically the appointment dates are repeatedly assigned to each patient but only difference is patientid will be not null for actual patient record.
Create table sample (PatientID int null, AppointmentDate date null, PatientName varchar(30) null)
I am using the SqlDependency to notify me of data changes in a table. However, as soon as I start it, the OnChange event fires over and over even though no data has changed.
Essentially All I want to do is be notified when records are inserted into a table. I am able to get another example to work using a Service Broker Queue and a sql query of WAITFOR ( RECEIVE CONVERT(int, message_body) AS msg FROM QueueMailReceiveQueue ) However I would prefer not to use a queue for once my messages have been read they are taken off the queue and I would rather control that manually.
Any help with getting SqlDependency to notify my app when records are added and not over and over when the data has changed would be great.
Failed install on several Win 2003 Ent Ed., 32bit servers both named and default instances, both upgrades and direct installed versions. Why would MS put out such a riddle?
Error Message is " A recently applied update, KB921892, failed to install.
Also, confoundingly, the error log for the hotfix indicates ""9.00.3042.00 while the update version is: 9.00.2047.00."
But the version display on the properties of one of the servers is: "Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) Oct 14 2005 00:33:37 Copyright (c) 1988-2005 Microsoft Corporation Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2) "
Any help or comments are deeply appreciated, as the seems to be impacting DB Maintenance Plans, etc...
I have implemented an SSAS stored procedure for dynamic security and I call this stored procedure to obtain the allowed set filter. To my supprise, the stored procedure is being called repeatedly many times (more than 10) upon establishing the user session. Why is this happening?
i am inserting something into the temp table even without creating it before. But this does not give any compilation error. Only when I want to execute the stored procedure I get the error message that there is an invalid temp table. Should this not result in a compilation error rather during the execution time.?
--create the procedure and insert into the temp table without creating it. --no compilation error. CREATE PROC testTemp AS BEGIN INSERT INTO #tmp(dt) SELECT GETDATE() END
only on calling the proc does this give an execution error
If on the source I have a new column, the script generated by SqlPackage.exe recreates the table on the background with moving the data into a temp storage. If the table is big, such approach can cause issues.
Example of the script is below: in the source project I added columns [MyColumn_LINE_1] and [MyColumn_LINE_5].
Is there any way I can make it generating an alter statement instead?
BEGIN TRANSACTION; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SET XACT_ABORT ON; CREATE TABLE [dbo].[tmp_ms_xx_MyTable] ( [MyColumn_TYPE_CODE] CHAR (3) NOT NULL,
[Code] ....
The same script is generated regardless the table having data or not, having a clustered or nonclustered PK.
The SP UserPersist_GetByCriteria does a "SELECT * FROM tbl_User WHERE gender = @Gender AND culture = @Culture", so why am I receiving this error when both tables have the same structure?
The error is being reported as coming from UserPersist_GetByCriteria on the "SELECT * FROM tbl_User" line.
I'm having many many issues installing sql server management tools. i had visual studio 2005 installed first, but uninstalled and sql related things before trying to install sql server management tools again - i also deleted the program files/microsoft sql server/ folder so there are no references.
Firstly the system configuration check gives me a warning that the system doesn't meet the recommended hardware requirements - this is wrong... i've got 2.33Ghz dual core + 1gb of ram...
I select just management tools + client connectivity to install and click next -> the setup support files/native client/owc11 etc all install fine but workstation components etc fail and the setup log appears to either be empty and not available and MSXML6 fails... after clicking finish the installer appears to crash - : "Microsoft SQL Server 2005 Setup has encountered a problem and needs to close. We are sorry for the inconvenience"... I have tried all sorts of variations on this install and have had no problems in the past - please help!
The setup log from the MSXML6 failure - === Verbose logging started: 18/03/2008 12:34:09 Build type: SHIP UNICODE 3.01.4000.4039 Calling process: C:Program FilesMicrosoft SQL Server90Setup Bootstrapsetup.exe === MSI (c) (5C:78) [12:34:09:067]: Resetting cached policy values MSI (c) (5C:78) [12:34:09:067]: Machine policy value 'Debug' is 0 MSI (c) (5C:78) [12:34:09:067]: ******* RunEngine: ******* Product: {AEB9948B-4FF2-47C9-990E-47014492A0FE} ******* Action: ******* CommandLine: ********** MSI (c) (5C:78) [12:34:09:067]: Client-side and UI is none or basic: Running entire install on the server. MSI (c) (5C:78) [12:34:09:067]: Grabbed execution mutex. MSI (c) (5C:78) [12:34:09:067]: Cloaking enabled. MSI (c) (5C:78) [12:34:09:067]: Attempting to enable all disabled priveleges before calling Install on Server MSI (c) (5C:78) [12:34:09:067]: Incrementing counter to disable shutdown. Counter after increment: 0 MSI (s) (28:E4) [12:34:09:113]: Grabbed execution mutex. MSI (s) (28:74) [12:34:09:113]: Resetting cached policy values MSI (s) (28:74) [12:34:09:113]: Machine policy value 'Debug' is 0 MSI (s) (28:74) [12:34:09:113]: ******* RunEngine: ******* Product: {AEB9948B-4FF2-47C9-990E-47014492A0FE} ******* Action: ******* CommandLine: ********** MSI (s) (28:74) [12:34:09:113]: Machine policy value 'DisableUserInstalls' is 0 MSI (s) (28:74) [12:34:09:113]: MainEngineThread is returning 1605 MSI (c) (5C:78) [12:34:09:113]: Decrementing counter to disable shutdown. If counter >= 0, shutdown will be denied. Counter after decrement: -1 MSI (c) (5C:78) [12:34:09:113]: MainEngineThread is returning 1605 === Verbose logging stopped: 18/03/2008 12:34:09 ===
The log summary:
Microsoft SQL Server 2005 9.00.1399.06 ============================== OS Version : Microsoft Windows XP Professional Service Pack 2 (Build 2600) Time : Tue Mar 18 12:05:06 2008
EOC429 : The current system does not meet recommended hardware requirements for this SQL Server release. For detailed hardware requirements, see the readme file or SQL Server Books Online. Machine : EOC429 Product : Microsoft SQL Server Setup Support Files (English) Product Version : 9.00.1399.06 Install : Successful Log File : C:Program FilesMicrosoft SQL Server90Setup BootstrapLOGFilesSQLSetup0001_EOC429_SQLSupport_1.log -------------------------------------------------------------------------------- Machine : EOC429 Product : Microsoft SQL Server Native Client Product Version : 9.00.1399.06 Install : Successful Log File : C:Program FilesMicrosoft SQL Server90Setup BootstrapLOGFilesSQLSetup0001_EOC429_SQLNCLI_1.log -------------------------------------------------------------------------------- Machine : EOC429 Product : Microsoft Office 2003 Web Components Product Version : 11.0.6558.0 Install : Successful Log File : C:Program FilesMicrosoft SQL Server90Setup BootstrapLOGFilesSQLSetup0001_EOC429_OWC11_1.log -------------------------------------------------------------------------------- Machine : EOC429 Product : Microsoft SQL Server 2005 Backward compatibility Product Version : 8.05.1054 Install : Successful Log File : C:Program FilesMicrosoft SQL Server90Setup BootstrapLOGFilesSQLSetup0001_EOC429_BackwardsCompat_1.log --------------------------------------------------------------------------------
SQL Server Setup failed. For more information, review the Setup log file in %ProgramFiles%Microsoft SQL Server90Setup BootstrapLOGSummary.txt.
I want to insert the data from temp table to other table. Only condition is, it needs to sorted based on tool number and tool date. For example if we have ten records for tool number 1000, it should be order by tool number and then based on tool_dt. Both tables doesn't have any primary keys. Please find below my code. I removed all the unnecessary columns for simple understanding. INSERT INTO tool_summary (tool_nbr, tool_dt) select tool_nbr, tool_dt from #tool order by tool_nbr, tool_dt...But this query is not working as expected. Data is getting shuffled.