Prior Sql Queries
Mar 1, 2006Is there any way to check 1-2 days prior sql queries executed..??
However, i am going for auditing now.
Thanks,
Pavan
Is there any way to check 1-2 days prior sql queries executed..??
However, i am going for auditing now.
Thanks,
Pavan
Hello,
I have to migrate an application from oracle to sqlserver but i habe query with
connect by prior
What is the equivalent in sqlserver 2000.
Please help me.
Hello, Is there a way to do a prior omit in a SELECT?
I currently have multiple selections separated by a GO, but I need to prior Omit the rows I selected prior
so I have
select * from mytabe where
Address <> ' ' and
Zip4 <> ' ' and
zip in ('12345')
Go
select * from mytabe where
Address <> ' ' and
Zip4 <> ' ' and
Purchcode in ('A','C')
Go
This continues for another 10 selections I need to constantly omit the selections made above.
Is there a way to do this in SQL?
Thanks!
I'm trying to find out if a certain row exists in my table, if it does exist then I do nothing if it doesn't exist I will insert a value. Only problem is i'm quite new and not sure how to get the values from the insert to correctly check if they already exist. Heres my attempt using query analyser
DECLARE @FK_UserID int, @NoteID int, @BeenRead NVARCHAR(10), @TMPuserID INT, @TMPnoteID INT
SET @FK_UserID = 1
SET @NoteID = 254
SET @BeenRead = 'MyTestHere'
SELECT DISTINCT Backup_UserNotes.BackUp_UserID, Backup_UserNotes.BackUp_NoteID
FROM User_Notes INNER JOIN
Note ON User_Notes.FK_UN_NoteID = Note.NoteID INNER JOIN
Backup_UserNotes ON Note.NoteID = Backup_UserNotes.BackUp_NoteID
WHERE BackUp_UserID = @FK_UserID AND BackUp_NoteID = @NoteID
SELECT @NoteID = @@IDENTITY
SELECT @FK_UserID = @@IDENTITY
IF @FK_UserID = NULL AND @NoteID = NULL
BEGIN
INSERT BackUp_UserNotes (
BackUp_NoteID,
BackUp_UserID,
BackUp_Read)
SELECT @NoteID, @FK_UserID, @BeenRead
END
-- RETURN 0
------------------------------------------------
GO
Anybody know the best solution? Thanks
I can't figure this out for the life of me. Wanted to know if it's possible to select certain date conditions in a query, then later reference those conditions and to only select the max of them.
I need to do this dynamically as I do not know what the max value is. I've provided an example below:
Select var1
From table1
where
(
(Date1 = '11/30/2005')
OR
(Date1 = '12/31/2005')
)
and Date1 = (Max of previously selected values e.g. '12/31/2005')
What I can't figure out is how to dynamically retrieve the max of 11/31/2005 and 12/31/2005. Any ideas are greatly appreciated.
Hello gurus, I have a table of data containing stop and start times of equipment, such as this:
StartTime....................EndTime
12/01/01 15:44...........12/01/01 18:44
12/02/01 3:44............12/02/01 14:44
12/02/01 15:22...........12/02/01 15:33
etc.
With this, I can build a query that subtracts the start and end dates to give me the total differences between each record... But, how do I write a query that can count the elapsed time between the current record's "start time" and the prior record's "end time" to give me the elapsed time between each record?
One more question.
I need to use select using some type of a prior omit.
for example, I have 2 selects:
select * from table abc where zip in ('07661') and name like 'A%'
go
select * from table abc where zip in ('07661') and company like 'ABC&'
there could be common records in both select statements, but I want to exclude the same records from the first select when I run my second one.
I could say
select * from table abc where zip in ('07661') and company like 'ABC&' and name not like 'A%'
in my second select, but I rather not do that, since my queries has many select criterias and its not just 2 selects.
So is there another option? a subsitution for "go"?
thanks
Hello,I have an Access DB with a column [date] holding dates from present day backto around year 1702, and I am trying to run DTS Import with this MDB, whichfails due to a conversion error.MSDN says the datetime data type only allows 1753-01-01 to 1900-01-01, thusI am under the impression having the [date] field as varchar is the only wayto store the dates.My questions are:a) Can dates prior to 1753-01-01 be stored in a date column?b) If dates are in a column of type varchar (or similar) is it possible tosort them chronologically with T-SQL ?c) 1753-01-01 seems particularly arbitrary, any reason for this?Many thanks for any help, it's driving me mad!John
View 6 Replies View RelatedI have a scheduled job that will do a database restore at given time every day. Sometimes I run into a situation where some people leave themselves logged on to the database, which prevents the job from running.
Is there a way that I can set up my job to include killing any open processes against the database that I'm restoring prior to the restore being done?
I've set up a SQL7 database with MSAccess97 as a front end. I'm trying to enter a person with a birthdate prior to 1900, get an ODBC call error, "Datetime field overflow". How to enter dates prior to year 1900?
Thanks.
This is quite a tricky one for me to figure out.
I have a datetime field in a databse for when a tender closes. More often then not its 4pm - for what its worth. And I want to send an email informing the manager 6hrs, 30mins prior and when the tender closes. So I need to somehow check that field either once a day and store those times somewhere or quite regularly ... like every hour.
I would be interested to hear how people would approach such a concept. I havn't got a great deal of experience in triggers or SQL mail.
It is in an asp file that I am doing it.
Any ideas would be hugely appreciated.
Cheers
tom
I am trying to make a connection to sql server using sspi (windows authentication), however, I do not wish to use the built in tokenauthentication system. Administrators are worried about somebody using an unattended logged on computer to gain access to the database. So I am left with using sql server authentication (which I am not fond of doing, more password administration) or what I would like to do is use windows authentication but make the user enter thier windows password prior to logging into sql server. Is there a way? Thanks in Advance.
Kent
This is for SQL 2005 and I know how to right click an index and do a "script index as create to new query window"
Basically, the one of the Microsoft scripts or views will tell us that we have 100+ indexes that exist for a database but that they are not being used by SQL server.
I will probably take them "offline" for a while and then drop them later on.
Before dropping them, I would like to be able to recreate them within minutes if system performance is degraded after this happens.
I was working on a script to pull this info out of the sys. tables like below but was wondering if anyone already has a script or an easier way to do this. (...and I don't want to right click 100 indexes within 137 tables and script to a new window and then compile a big script)
SELECT
'Create ' +
i.type_desc COLLATE SQL_Latin1_General_CP1_CI_AS +
' Index [' +
i.name +
'] ON ' +
t.name +
CHAR(10) +
c.name +
' ASC'
FROM sys.index_columns ic
Join sys.indexes i ON i.index_id = ic.index_id
JOIN sys.tables t ON ic.object_id = t.object_id
Join sys.columns c ON ic.column_id = c.column_id
WHERE
i.name = 'IX_Sellers_StatusID' and
c.object_id = t.object_id and
i.object_id = ic.object_id
Thanks,
Brian
I have a text file I need to zip (compress) before uploading it via an FTP task. Do I have to use a third-party tool to do this? Thanks.
View 15 Replies View RelatedI have to create a report and I want all activity for the previous month.
I need to calculate the First and Last Day Prior Month to be used as Input Parameters.
Would something like this be the case or is there a better solution?
[code="sql"]
SELECT DATEADD(month, DATEDIFF(month, -1, getdate()) - 2, 0) as FirstDayPreviousMonthWithTimeStamp,
DATEADD(ss, -1, DATEADD(month, DATEDIFF(month, 0, getdate()), 0)) as LastDayPreviousMonthWithTimeStamp
[/code]
I was thinking get the first day of the previous and current month to exclude the Timestamp and use a less then first day of current month?
I have a situation where our stored procedure inserts records from table_1 to table_2 when they don't already exist (uses the EXIST statement) on that table. If table_1 contains multiple records that are the same, it appears after the 1st record has been inserted, it does not recognize it as being there when it checks the existence when attempting to insert record 2.
Here's an example of the script:
insert into table_2 (col1,col2,col3)
select col1,col2,col3
from table_1 t1
where not exists (select '1'
from table_2 t2
where t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3)
Data from Table_1 -- Assume that table_2 does not contain these records
col1 col2 col3
AA 11 A1
AA 11 A1
BB 22 B2
All 3 records would be inserted to table_2 in this example.
We were asked to fix a query to get rows from a prior year history table that did not match to rows in the current year to show a variance from one year to the next. Rows must match on [corpnbr],[plincd],[pgrpcd] and [pitmcd]. If the combination has rows in the current and prior year ([hstyr]) then everything is fine. However, if they have rows in the prior year (e.g. [hstyr]='2014') but not in the current year (e.g. [hstyr]='2015') then they do not show in the result. Below is how they designed the table and below that is the stored procedure to pull the records.
CREATE TABLE [dbo].[BillingHistory](
[BillingHistoryID] [int] IDENTITY(1,1) NOT NULL,
[entity] [varchar](4) NULL,
[plincd] [varchar](3) NULL,
[pgrpcd] [varchar](4) NULL,
[pitmcd] [varchar](4) NULL,
[newplincd] [varchar](2) NULL,
[Code] ....
Hello ,
I'm facing a complicated problem and I don't think that the solution will be an easy one.
I have an SQL statement in Oracle which I need to translate it in Sql Server 2005.
select lpad(' ',5*(orderid)) || to_char(descr) as menui
from <table _name>
where MENU_ITEM not in ('test1','test2','test3') and item_parent not in ('test4,'test5,'test6')
start with <item_parent='item_parent' >
connect by prior <menu_item = item_parent and menu_name='ADR_m_adr_frame'; > ?(condition)
Somewhere I have read that SQL server does not support Hierarchical Sql statements. Is this true ? How am I going to do that ?
Any help will be appreciated.
Thank you
Hello folks,
I am trying to set up replication between a central office and multiple sites. This will be merge repl, since either central or satellites can be modified. However, one issue I ran into is setting up subscribers. I don't always have the option of starting with a publisher's snapshot; some of the subscribers have their own data already, which may or may not overlap with central site, and which needs to be synchronized into the central site somehow.
So far, I haven't seen any solution for this kind of scenario, at least nothing out of the box. Am I doomed to having to roll out custom data transfers (I don't think I can use DTS since all sites are running Express), or is there some standard way of doing this?
Thanks so much!
Sonya
My overall problem is to do some string manipulation and then DATEADD, but I still don't know why I getting what SQL is giving me.
To get the prior month, I use the following code;
SELECT
(
SELECT
RIGHT('00' + CAST(
CASE CAST(RIGHT(MAX(Eff_Period), 2) AS int) - 1
WHEN 0 THEN 12
ELSE CAST(RIGHT(MAX(Eff_Period), 2) AS int) - 1
END AS varchar(2)), 2)
)--, -- Month
--CAST(CAST(LEFT(MAX(Eff_Period), 4) AS int) - 1 AS varchar(4)) -- Year
FROM
Current_Membership;
This correctly gives me the proper month number for the prior month, in this case, '06' since the current month is '07.'
But, for debugging, I wanted to test to make sure it would properly adjust for January - '01' - by replacing MAX(Eff_Period) with '201501.'
It did. Worked fine. Then, to make really sure, I put in '201502.'
Instead of the single record of '01,' it shot out 177,209 rows of all '02's!
I put back in the MAX(Eff_Period) instead of the date string constant and, worked fine. Swapped back in the date string - boom! - 177,209 rows again.
Like I said, I can use DATEADD - Now I'm really confused. I changed the date string to '201501' just to verify before posting this, and it spit out 177,209 rows of '01's! So, to keep from altering things and to maintain the edit history, I started up another query and C&P'd everything to it. Then, on the original, I backed up to an earlier version, one that worked. That listing is the same as above, but the commented out comma in line 9 and the commented CAST in the line after it - to get the year - were uncommented.
Now, it works with both '201501' and '201502.' But, if I replace the comments, basically removing the CAST with the year, it slams back with 177,209 rows!
I have a maintenance plan set to back up my 2 primary databases, one about 1 GB, the other about 2GB. I have about 4 GB available on the back up drive. I have set it to delete old backups more than 1 minute old. Problem is, the first night it works fine, the second night it sometimes it refuses to delete the old and therefore does not have enough space to complete the new back-up. Does anyone run into this before? Does it complete both back-ups THEN erase the old, in which case I need to have about 6GB available? Sometimes it works and sometimes it doesn't. It can be going along fine for weeks then stop working. Any info or links to how this is supposed to work would help.
TIA
Hi
I'm using two different server for application(.net version 1.1) and database(sqlserver200) with win2k3 environment. I'm getting the below error message all the times. I've veryfied the communication between the two server is fine.
"The timeout period elapsed prior to completion of the operation or the server is not responding"
Any help is greatly appreciated
Cheers
Veeru
I have a table (represented by #Events) that holds modifications made to another table. I do have some control over the table structure and indexing. I want to pull all of the change records that were made between two dates.
The tricky part is to include the previous version of each record, which will usually be found prior to the start date in question.
The code that I have provided below works. So you can use it to easily see what should be returned. But it's very slow in production.
Any better method to pull this data together?
-- Production version of this table has 4.5 million rows (roughly 1,000 rows per day)
-- Primary key is on L4Ident (clustered)
-- nonclustered index on ProcessDate, LinkRL4
DROP TABLE dbo.#Events;
DROP TABLE dbo.#Results;
CREATE TABLE dbo.#Events (
L4Ident int IDENTITY(1,1) NOT NULL,
[Code] ....
I have data similar to the below
CREATE TABLE #TEMP
(
TYPE VARCHAR(10),
SEQ INT,
SUB_TYPE VARCHAR(10))
[Code] ....
Now for each type the seq is very important. Effectively by order of seq the subtype stays the same until another subtype changes it. So for TYPE1 100,110 and 150 are A. 170, 200,220 are B. 230 and 250 are C and so on.
However as you can see the data isnt actually stored in the row. I need a select statement that shows this data.
I have done this:
SELECT t1.*,t3.SUB_TYPE FROM #TEMP t1
CROSS APPLY
(SELECT MAX(SEQ) SEQ FROM #TEMP AS t2 WHERE t1.SEQ >= t2.seq AND t2.SUB_TYPE <>'' AND t1.TYPE = t2.TYPE
GROUP BY t2.TYPE) t2
INNER JOIN
#TEMP t3
ON t3.TYPE = t1.TYPE AND t2.SEQ = t3.SEQ
And it seems to work. Is this the easiest way to do it or am i missing something?
I have some my below requirment to loading some last year and currnet year records for some ID's in my table,
We have to load the ID's that are active at the end of the year for the prior year and ID's that are active as of today for the current year.Here is the scenario when the ID is currently terminated but active at the end of the prior year and the record is not in the table.so, we didn’t load the count for the prior year
Here prior year is 2015-2015 and Current year is 2015-2016
CREATE TABLE remp_year
(ID INT,
STATUS NVARCHAR(100) NULL,
START_DATE DATE NULL,
END_DATE DATE NULL,
date_year nvarchar(10) NULL)INSERT INTO remp_year VALUES (10,'Active','2015-05-26','2015-12-31','2015-2016');
[Code] ...
Here ID 20 and 50 for terminated records is the prior year records so it should count for the last year and those are active in this year those will count for this year.
Hello,
I would like a way to check a file before attempting to restore it. I want to be able to tell that this is a valid SQL backup file and not just some random file before I attempt a restore.
Any help would be appreciated!
hassiah
I’m using DAX to calculate the prior MTD count of a specific column. My data ends on 2/8/2013 and that day's PriorMTD is incorrectly corresponding to 1/31/2013. Whereas, the previous 7 days in February correctly match their corresponding January dates..Below is an image of my pivot table and I have outlined the values in red that are in question.Below are my DAX formulas used each column visible in my image:
Distinct Count of Events:=DISTINCTCOUNT([EventID])CurrentMTD:=CALCULATE([Distinct Count of Events], DATESMTD(Events[EventDate]), ALL (dimDate) )PriorMTD:=CALCULATE([Distinct Count of Events], DATEADD(DATESMTD(Events[EventDate]), -1, MONTH), all(dimDate)) ParallelMonth:= CALCULATE ([Distinct Count of Events], ParallelPeriod(Events[EventDate], -1, MONTH), ALL(dimDate))
Hello,
I have some C# (.NET 2.0) code that performs back-ups and also can perform restores of back-up files. I would like to validate the integrity of a file prior to initiating a restore operation. In other words, I want to know, before I try to restore, if the file is just a random file (picture, work doc, zipped backup, etc.) or if it is a valid SQL backup file.
Any help would be greatly appreciated.
Thanks,
hassiah
I need to run a select on Mondays to pull data for 7 days prior to the Thursday of last week; i.e. Friday - Thursday inclusive. I'm sure this is simple, but I work with dates so infrequently that I need a refressher.
View 7 Replies View RelatedI have a table that contains a column called hardness. It is a varchar 50 and actually contains numbers. I did not create the database so not sure why they choose varchar vs. number. I need to write a query that will return all results with a space prior to the number in this column. I am having difficulty and would greatly appreciate some help. Thanks!
View 6 Replies View RelatedI want to dynamically build a page for a given stored proc.
Given the name of the sproc how can i return the names, datatypes & sizes of the parameters ?
I then needs to find the fields in the result set to generate the page.
Any ideas ?
Just Like these:
-- batch start
Select * from someTable --maybe a query which need much res(I/O,cpu,memory)
/*
can I do something here to free res used by prior statement?
*/
select * from someOtherTable
--batch end
The Sqls above are written in a procedure to automating test for some select querys.
I have two tables - one with dates at the end of each month (10/31, 11/30, 12/31, et al) that's linked to a data table containing client signups. While I can get the count per month in a pivot table, I'm trying to calculate the beginning count of each month.
How do I use calculate or something similar to sum the count of records created minus count of records closed prior to the beginning of the period?
The pivot looks like this right now and works by month:
beginning active records ?????
count of records created 100
count of records deleted 50
net records created 50
running balance in net records created 100 (theoretically the next month's beginning balance, but can't figure out how to reference or calculate)