Hi guys. I'm looking for help to run a query that should come out looking like this
unit reports_to
Development Dept Representative Div
Systems Group Development Dept
Design Section Systems Group
Production Spec Section Systems Group
Proposal Section Systems Group
I can't see any way of doing this query though I know the PARENT is the reports_to column and that the UNITNO is the Department but can't find any way to run the query. Hoping you guys can help
If I start a long running query running on a background thread is there a way to abort the query so that it does not continue running on SQL server?
The query would be running on SQL Server 2005 from a Windows form application using the Background worker component. So the query would have been started from the background workers DoWork event using ado.net. If the user clicks an abort button in the UI I would want the query to die so that it does not continue to use sql server resources.
I hope I am not asking about something that has been done before, but Ihave searched and cannot find an answer. What I am trying to do is torun a query, and then perform some logic on the rowcount and thenpossibly display the result of the query. I know it can be done withADO, but I need to do it in Query Analyzer. The query looks like this:select Varfrom DBwhere SomeCriteriaif @@Rowcount = 0select 'n/a'else if @@Rowcount = 1select -- this is the part where I need to redisplay the resultfrom the above queryelse if @@Rowcount > 1-- do something elseThe reason that I want to do it without re-running the query is that Iwant to minimize impact on the DB, and the reason that I can't useanother program is that I do not have a develpment environment where Ineed to run the queries. I would select the data into a temp table, butagain, I am concerned about impacting the DB. Any suggestions would begreatly appreciated. I am really hoping there is something as simple as@@resultset, or something to that effect.
I have an update query running which to just now has been running for 22 hours running on two tables 1 a lookuptable that has just been created within the batch the other a denormalised table for doing data analysis on
the query thats causing teh problem is
--//////////////////////////////////// this is the one thats running
Print 'Update Provider 04-05 EmAdmsCount12mths : ' + CAST(GETDATE() AS varchar) GO Update Provider_APC_2004_05 set EmAdmsCount12mths = (Select COUNT(*)-1 from Combined_Admissions where ((Combined_Admissions.NHSNumber = Provider_APC_2004_05.NHSNumber) or (Combined_Admissions.PASNUMBER = Provider_APC_2004_05.PDDISTNO)) and (Combined_Admissions.AdmDate BETWEEN DateAdd(yyyy,-1,Provider_APC_2004_05.AdmDate) AND Provider_APC_2004_05.AdmDate) AND Combined_Admissions.AdmMethod like 'Emergency%')-- and -- CA.NHSorPrivate = 'NHS')) FROM Provider_APC_2004_05, Combined_Admissions
any help in improving speed would be most welcome as there are 3 more of these updates to run right after this one and the analysis tables are almost double the size of this one
1) is there any way to run a query over a query without having to create a table with the results of the first query? (would drop table work? If so, how?
2) how can i define input variables the same way i do in excel? I am trying to run a couple of simulations based on 2 core inputs (in excel i would just do a data table)
Presumably, an RDL data set is only queried once before generating a report. And presumably, this is true even if the data set feeds a list control. However, I'd like to know if I can cause a data set to be queried repeatedly from a list control. I need to do this, because the data set contains aggregate functions that would be impacted by each "pass" the list control would make.
Is it possible to make a data set queried repeatedly in this fashion?
I have a pretty complex query that aggregates lots of data and inserts multiple rows of that data into a reporting table. When I call this SPROC from SQL Server Management Studio, it executes in under 3 seconds. When I try to execute the same SPROC using .NET's SqlCommand object the query runs indefinitely until the CommandTimeout is reached. Why would this SPROC behave differently with the same inputs, but being called from .NET? Thanks for your help!
Ok, here's a funky one That I can't find an expanation for. If I go into EM and choose a table from a database and return all rows, I get immediate results and can start browsing records. If I go into query analyzer and do a select * on the same table, it takes up to 20-25 minutes to return the result set. This used to only take like 5 mins. What gives? Anyone seen this before?
I have two SQL server one in the office and other on remote location. I have some data on SQL in the office and some data on remote location and need to query the data.
SQL Server 7 When i ran a query in query analyzer i am getting the below error. once i closed and opened the query analyzer and ran the same query it worked fine.
pls the error which i got in first time.
Microsoft][ODBC SQL Server Driver]Unknown token received from SQL Server [Microsoft][ODBC SQL Server Driver]Protocol error in TDS stream [Microsoft][ODBC SQL Server Driver]Protocol error in TDS stream [Microsoft][ODBC SQL Server Driver]Protocol error in TDS stream [Microsoft][ODBC SQL Server Driver]Protocol error in TDS stream [Microsoft][ODBC SQL Server Driver]Protocol error in TDS stream [Microsoft][ODBC SQL Server Driver]Protocol error in TDS stream [Microsoft][ODBC SQL Server Driver]Protocol error in TDS stream [Microsoft][ODBC SQL Server Driver]TDS buffer length too large [Microsoft][ODBC SQL Server Driver]TDS buffer length too large [Microsoft][ODBC SQL Server Driver]TDS buffer length too large [Microsoft][ODBC SQL Server Driver]TDS buffer length too large [Microsoft][ODBC SQL Server Driver]Unknown token received from SQL Server [Microsoft][ODBC SQL Server Driver]Unknown token received from SQL Server [Microsoft][ODBC SQL Server Driver]Unknown token received from SQL Server [Microsoft][ODBC SQL Server Driver]Unknown token received from SQL Server [Microsoft][ODBC SQL Server Driver]Unknown token received from SQL Server [Microsoft][ODBC SQL Server Driver]TDS buffer length too large [Microsoft][ODBC SQL Server Driver]TDS buffer length too large [Microsoft][ODBC SQL Server Driver]TDS buffer length too large
Can someone please let me know how i can set up a query to execute once a week lets say on tuesdays at 6 am... i do not see how i can set this. can anyone help me please
On this server I have a few databases. I need to run the below query against each database. Since the server has approx 50 databases this will take time.
Is there any way for me to modify this so as it will automatically run against each database one at a time and then create a single output file with all the results.
I do not want it to run against all of the databases at the same time as it will kill my server so it must run and gather results on each database at a time.
QUERY to be run
SELECT RelativeFileName
FROM Collection, Saveset
WHERE Collection.CollectionIdentity = Saveset.CollectionIdentity
Hello all, Can anyone please tell me what query to use for joining two tables in two databases together?
Basically, we have two databases called 'system' and 'site' respectively. The 'system' database has a table called 'users' and each user has a unique id which is also linked to the tables in the 'site' database.
The only problem i have got is, I want to be able to get the actual user names not the unique id and the only way to get this from my SQL statement is to be able to link the uniqueid for each user in the 'site' database to their corresponding uniqueid in the 'system' database.
Hi,I have a DLL that I want to run from the Query Analyzer. I tried thefollowing:USE master;EXEC sp_addextendedproc BLAH, '\Other-ServerPathToBlahBlah.dll'and I get the error:ODBC: Msg 0, Level 16, State 1Cannot load the DLL \Other-ServerPathToBlahBlah.dll, or one ofthe DLLs it references. Reason: 126(The specified module could not befound.).The DLL was written in Cobol. The "Other-Server" in the Path above isthe server that Cobol (and the DLL) is located on.I looked at the dependencies for the DLL and it includes (notsurprisingly) Cobol Dll's. It should find those on the other server.What can I do?Thanks!
I'm trying to optimize a long running (several hours) query. This query is a cross join on two tables. Table 1 has 3 fields - ROWID, LAt and Long. Table 2 has Name, Addr1,Addr2,City,State,Zip,Lat,Long.
Both tables has LatRad - Lat in radians, LonRad- Lon in Radians. Sin and Cos values of Lat and Lon are calulated and stored to be used in the distance formula.
What I'm trying to do here is find the nearest dealer (Table 2) for each of Table 1 rows. The Select statement takes a long time to execute as there are about 19 million recs on table 1 and 1250 rows in table 2. I ran into Log issues- filling the transaction log, so I'm currently using table variables and split up the process into 100000 recs at a time. I cross join and calculate the distance (@DistValues) and then find the minimum distance (tablevar2) for each rowid and then the result is inserted into another Table (ResultTable).
distance=3963.1*Case when cast(S.SinLat * T.SinLat + S.CosLat * T.cosLat * cos(T.Lonrad - s.Lonrad) as numeric(20,15)) not between -1.0 and 1.0 then 0.0 else acos(cast(S.SinLat * T.SinLat + S.CosLat * T.cosLat * cos(T.Lonrad - s.Lonrad) as numeric(20,15))) end
from dbo.TopNForProcess T , dbo.Table2 S where Isnull(T.Lat,0) <> 0 and Isnull(T.Lon,0)<> 0
Insert into @MinDistance
Select DataSeqno,Min(distance) From @DistValues Group by DataSeqno
Insert into ResultTable (DataSeqno,Lat2,Lon2,StoreNo,LAt1,Long1,distance)
Select D.DataSeqno, D.Lat2, D.Lon2, D.StoreNo, D.LAt1, D.Long1, M.distance from @DistValues D Inner Join @MinDistance M on D.DataSeqno = M.DataSeqno and D.Distance = M.Distance
I created a View called TopNForProcess which looks like this. This cut down the processing time compared to when I had this as the Subquery.
SELECT TOP (100000) DataSeqno, lat, Lon, LatRad, LonRad, SinLat, cosLat, SinLon, CosLon FROM Table1 WHERE (DataSeqno NOT IN (SELECT DataSeqno FROM dbo.ResultTable)) AND (ISNULL(lat, 0) <> 0) AND (ISNULL(Lon, 0) <> 0)
I have indexes on table table1 - Rowid and another one with Lat and lon. Table2 - Lat and Long.
Is there any way this can be optimized/improved? This is already in a stored procedure.
I have a query that has cursor loop. in each loop there is many sql update commands. it runs good in sql2000.
now when i run it in sql2005 it continue for a long time and not finish. when i run each command that within the loop alone it runs good, but when i run it all together its not finish running. i dont get any error. i think maybe the table that i'm bulk updating is in some lock and this is why it stuck in the running and not finish.
Is there a way to make sure that there is no lock on a table before i update it during my running.
I have a simple query (a select statement which retrieves results from 2 tables via join). I run this query every week and just copy the results from Management Studio (Ctrl-A) and then open Microsoft Excel and just paste it there. Is there a way I can automatically run this every week and generate the Excel from the results? If so what steps I have to do? I use SQL Server 2005 Express Edition.
Hey, I have created a webpage using visual web developer and i want to use it as a frontend to a database. I want to be able to insert a new line into a table using a drop down menus then a 'submit' button. How do I get the submit button to run the following sql query? insert into master_training_table (user_rec_no,Course_rec_no,Planned_date,Inserted_date) select @user_rec_no,id_training,convert(smalldatetime,convert(varchar(12),getdate()))+planned_date,getdate() from tbl_training tb join tbl_user_groups tg on tb.groups_id = tg.groups_id where tg.groups_id = @groups_id thanks for your help
where 'Testaccess' is the name of the linked server and 'test' is the name of the ODBC connection. [The ODBC connection stores the name of the Access datbase.]
I can run sp_tables_ex and view the table_schem, table_cat etc etc
BUT when I try to run a query on the linked server, I get the following message:
"7312 - Invalid use of schema and/or catalog for OLE DB provider '%ls'. A four-part name was supplied, but the provider does not expose the necessary interfaces to use a catalog and/or schema."
I am attempting/using the following command specifying the linked server name.database name.owner.table name in the FROM clause:
Hi Can somebody suggest optimization on this query. ITs running for hours,I have checked on mutiple Servers but dont know where the problem is.It starts when i add the not exists piece if the query. Pls help
SELECT distinct isnull(P.id,'') PersonID , s.status , isnull(replace(Ltrim(Rtrim(P.Firstname)),',',''),' ') FirstName , isnull(replace(Ltrim(Rtrim(P.MiddleName)),',',''), '') MiddleName , isnull(replace(Ltrim(Rtrim(P.lastname)),',',''),'' ) LastName , isnull(replace(Ltrim(Rtrim(P.Suffix)),',',''),'') Suffix , isnull(replace(Ltrim(Rtrim(p.company)),',',''),'') Company , isnull(replace(Ltrim(Rtrim(PA.AddressLine1)),',',' '),'') AddressLine1 , isnull(replace(Ltrim(Rtrim(PA.AddressLine2)),',',' '),'') AddressLine2 , isnull(replace(Ltrim(Rtrim(PA.City)),',',''),'') City , State = isnull(replace(Ltrim(Rtrim(PA.State)),',',''),'') , ZipCode = ISNULL(CASEWHEN LEN((LTRIM(RTRIM(PA.Zipcode)))) = 3 THEN ('00' + (PA.Zipcode)) WHEN LEN((LTRIM(RTRIM(PA.Zipcode)))) = 4 THEN ('0' + (PA.Zipcode)) WHEN SUBSTRING(LTRIM(RTRIM(PA.Zipcode)),6,1) = '-' THEN ISNULL(PA.Zipcode,'') WHEN SUBSTRING(PA.Zipcode,6,1) = ' ' THEN ISNULL(PA.Zipcode,'') ELSE substring(PA.Zipcode,1,5)+'-'+ substring(PA.Zipcode,6,4) END ,'') , b.code , CommitteeTermID = '' , RevenueCategoryid = '' FROM vwpersons p with (nolock) join vwsubscriptions s on p.id = s.recipientid join vwcompanies c WITH (NOLOCK) on c.id = p.companyid join vwbusinesscodes b WITH (NOLOCK) on b.id = c.primarybusinesscode join vwRevenueCategorys rc WITH (NOLOCK)on rc.id = c.RevenueCategoryid join vwPersonPrefAddr pa WITH (NOLOCK) on p.id = pa.id WHERE NOT EXISTS (SELECT s1.recipientid FROM vwsubscriptions S1 WHERE S1.SHIPTOID =S.SHIPTOID AND S.PRODUCTID = S1.PRODUCTID AND S1.STATUSID IN (1,2,3,11,12)) and p.id in (select recordid from vwTopicCodeLinks tcl Where tcl.entityid = 457 And tcl.topiccodeid = 332 And tcl.value = 'Yes') and ((p.PreferredAddress = 'Business Address' and p.BadBusinessAddress = 0) or (p.PreferredAddress = 'PO Box Address' and p.BadPOBoxAddress = 0) or(p.PreferredAddress = 'Home Address' and p.BadHomeAddress = 0)) and b.code in ( 'M2','V2','V5','W1','W2','W3','W4' ,'W5','W6','W7','W8','W9','WZ','X2','X3','X4') and p.status <> 5 and S.statusID IN (4,5,6,7,8,9,10,13) and S.productid in (23) group by b.code ,p.id , p.firstname,p.lastname,p.MiddleName,p.suffix, p.company,pa.addressline1,pa.addressline2,pa.city, pa.state,pa.zipcode, s.status
I'm having what you might call an optimisation issue - but I'm also not sure if my approach to this problem is right. I've spent the whole day trying various methods but none seem to be performing as admirably as I'd hoped.
Basically, here's the scenario:
1. Log files are being inserted into a SQL table via Log Parser 2.2. 2. I have a lookup table of ip address ranges to countries and other geographic data. 3. Once the log row has been inserted from Log Parser, I want to update the same log table with data from the lookup table.
The main problem seems to be the updating (the initial insert from Log Parser is lightning quick).
I initially wrote a trigger on AFTER INSERT on the log table that converted the actual user's IP address into IPNumber format using a simple algorithm, then pumped the IPNumber into a quick select query which retrieved the geolocation data. Then, in the same trigger, there was an update statement which basically said:
update dbo.Logs set [Country] = @Country where [IpNumber] = @IpNumber and [Country] = Null
Therein lies the problem. The update statement slows everything down to almost a standstill and also seems to obtain a lock on the table.
Critical factors:
1. The log table must remain readable. 2. The query must execute in seconds -- not over half hour :)
I've experimented with various combinations of indexing, so far to no avail.
I would like to setup a query where the user is firstly prompted to input the information e.g. if they are trying to locate a particular person from a telephone database they would be prompted to enter the surname for that person.
Hi all As you know when you run a piece of TSQL scrypt in Query Analizer at the bottom of the page , sql-server will show you the Time of completion of your code....is there any way to capture this time from SQL-Server environment and use it in the Front-End Application to inform the user?
The query show below is designed to use seasonal profiles to compute 53 weeks of forecast data and then from that compute the number of weeks of supply of each item at each location. The query works but the volume of data produced (20+M rows) is substantial. If I limit the CTE to a single location, it run is 2 seconds and returns 41,000 rows. But when run for all locations and items, it runs for more than 4 hours. Would I do better converting the CTE to a sub-query and adding an index to improve the performance of the main query?
WITH Forecast AS
(SELECT Location_Idx
,Item_Idx
,Week_Code
,(CAST(AnnualQty AS DECIMAL(9))/53.0)*[Profile] AS fcst
FROM dbo.FactReplenishmentProfile rp
INNER JOIN dbo.FactSeasonalProfile sp
ON sp.SeasonalProfile_Idx = rp.SeasonalProfile_Idx
)
SELECT fcst1.Location_Idx
,fcst1.Item_Idx
,fcst1.Week_Code
,fcst1.fcst AS WeekQty
,SUM(fcst2.fcst) AS CumQty
FROM Forecast fcst1
INNER JOIN Forecast fcst2
ON fcst2.Location_Idx = fcst1.Location_Idx
AND fcst2.Item_Idx = fcst1.Item_Idx
AND fcst2.Week_code <= fcst1.Week_Code
GROUP BY fcst1.Location_Idx,fcst1.Item_Idx,fcst1.Week_code,fcst1.fcst
I have a table that will be loaded over night everyday and I need to write a query on running value difference ?
List of Columns (ID, Branch ,Group, Date, Value)
ID Branch Group Date Value 1 A C 2015-06-01 10 2 A C 2015-06-02 15 3 A C 2015-06-03 25 4 A C 2015-06-04 20 5 B D 2015-06-01 20 6 B D 2015-06-02 25 7 B D 2015-06-03 10 8 B D 2015-06-04 20
I want the Output like below with a Running value difference in comparison to previous day.
ID Branch Group Date Value Running Value 1 A C 2015-06-01 10 10 2 A C 2015-06-02 15 05 3 A C 2015-06-03 25 10 4 A C 2015-06-04 20 -5 5 B D 2015-06-01 20 20 6 B D 2015-06-02 25 05 7 B D 2015-06-03 10 -15 8 B D 2015-06-04 20 10
Basically I need to compare the previous day and show the difference. How can I do this in SQL 2008 r2?