How To Improve A Sql Sentence With A Clause Not In
Nov 23, 2007
Hello,
I would like to know how can I change a "not in" clause with the same results in a SQL sentence in order to improve the performance of my SQL sentence.
So, I need to delete the "timestamps" the an user has done, which the difference between one stamping and the following one is less than 2 minutes for the inputs for entrances and I need to remove it when the "stampings" which the difference between an one stamping and the previous one is less than 2 minutes for exits.
Can you help me please? I will appreciate a lot your help.
Violation of PRIMARY KEY constraint 'PK_VTA020SAP2'. Cannot insert duplicate key in object 'VTA020SAP2'. The statement has been terminated.
INSERT INTO VTA020SAP2 SELECT VTA020SAP1.*, NULL as xCLIPRO, NULL AS xPAIS, NULL AS xCONFREI, NULL AS xCONCPER, NULL AS xMONEDA, NULL AS xFAX, NULL AS xTIPEXP, NULL AS xCASILLA, NULL AS xDIAENTR, NULL AS xPUNVEN, NULL AS xCIUDAD, NULL AS xREGION, NULL AS xCONDIC, NULL AS xINSPAG, NULL AS xCUMPLE, NULL AS xPERVIS, NULL AS ESTADO FROM dbo.VTA020SAP1 INNER JOIN VTA020 ON VTA020SAP1.CNRCLI = VTA020.CNRCLI AND VTA020SAP1.CNRCLI IN (SELECT MAX(VTA020SAP1.CNRCLI) FROM VTA020SAP1 GROUP BY VTA020SAP1.CNUECOD) AND (not EXISTS (SELECT VTA020SAP2.* FROM dbo.VTA020SAP2 INNER JOIN VTA020SAP1 ON VTA020SAP1.CNUECOD = VTA020SAP2.CNUECOD WHERE dbo.VTA020SAP2.CNUECOD = dbo.VTA020SAP1.CNUECOD))
If I want to change the value of a special bit in a column, how to wirte the SQL sentence in sqlserver. I mean in a column, the value is 'test', then how to change the 's' to 'e' or any other value by just one SQL sentence. Thanks.
I'm using the sentence NOLOCK for selects, but I have many sentences, Is there any way to set a parameter in the DBMS, to use NOLOCK parameter by default ???? I mean, I don't like to lock any table for selects.
Hi, all friends. I'm using SQL-SERVER 2000.I have a table like this:NIF Name----------------------------------------A-1234 Company1B-123-B1 Company2C-4568 Company4D-453-DF Company5I want delete the symbol "-" in the row "NIF". eg. A-1234 change toA1234, D-453-DF to D453DFSo how can I write the sentence . Should I use the Stored Procedure?Thank you very much!!Simon
i have insert/update SQL sentence, but sometimes there are empty values because there not required in the database so sometimes the sql sentence look this way:
INSERT INTO EquipmentAndPlace (EquipmentID,EquipmentEmdaNo,EquipmentPlace,EquipmentIDForRecognize, EquipmentRemarks,EquipmentLastChecked) VALUES ('3','','2','1','','12/1')
with empty values, but then it doent update in the dataBase-only if all the values appear- what the solution of it? Thanks
I have two tables. A table called users (content speaks by it self) and a table called groups. Since i want every user to be able to be a member of several groups and, of course, a group to have several users i have made a junction table called jt. The junction table contains userId's and groupId's according to the user-group bindings. The primary key(identity) in the junction table is a int named jtId. Now i want to take out all posts from the junctiontable and the corrresponding userName (s) from the users-table and the corresponding groupName from the groups-table. Can somebody please help me to make a SQL command that will di that for me. I have tried with INNER JOIN and several SELECTS in the same command. Thanks in advance, Greetings from Esben
I want to add a parameter to the following SQL-sequence. ActivityGroup is the table and I want to select the current activityGroupID in place of XXXXX. Do I need to put this in a stored procedure?
select * from ActivityGroup where cpgActivityGroupID = XXXXX and cgpRegistredFrom > getDate() and cgpRegistredTo < getDate()";
I have a project in which I have about 20,000 records in sql database table.
What I would like to do is generate a query that lists all the unique words in a particular field acros the entire table so as to generate a glossary of words.
if we had a table that looked like
ID Description
001 This is the first record
002 This is the second record
003 This is not the first record
and the query was run on the description field, then the result I would like to see is
This is the first second not I hope this makes sense. Any help is appreciated.
My MASTER table that contains text is very simple: TEXT_ID INT TEXT varchar
I was thinking of something like creating another table to keep track of selection criteria: USER_NAME varchar // keeps track of which user made the selection TEXT_ID INT // points to the text id in the MASTER table START_POS INT // start position of the selection END_POS INT // end position of the selection COLOR INT // color of the selection
The thing I don't like about this method, is if user(s) have many words higlighted in the sentences, this table may get very large.
I also thought of maybe combining START_POS, END_POS & COLOR into comma delimited entries:
EX: 1:5:240, 25:30:125 // START_POS:END_POS:COLOR
The thing I don't like about this method, the parsing calculations may take a while to execute.
My goal is to stream as much text data as possible and make it as fast as possible. How does Google or any one else do it?
P.S. Having a copy of a MASTER table with HTML tags in TEXT field is not an option.
I have to parse a sentence into separate words. This has to be done in a Stored Proc.
If the sentence is: "The sun is rising". Then I want to store the words "The" "sun" "is" "rising" into 4 separate variables. (The separator is a space).
Has someone already written something like this before? I do not want to re-invent the wheel.
CREATE TABLE [dbo].[instructions]( [site_no] [int] NOT NULL, [instructions] [text] NULL ) Select top 3 * from instructions
Output
Site_no Instructions 20 Request PIN#510 then proceed 21 Request PIN#987 if wrong request name 22 Request PIN#688 allowed to use only numbers
All text instructions start with “Request PIN#XXX” also after that the text are different for every site_no
I need insert in all site_no rows and after the “Request PIN#XXX” the text “and codeword” keeping the current rest of text
How can I set e REPLACE CAST sentence using something LIKE PIN%%%%
To get these type of results
site_no instructions ----------- ---------------------------------------------------- 20 Request PIN#510 and codeword then proceed 21 Request PIN#987 and codeword if wrong request name 22 Request PIN#688 and codeword allowed to use only numbers
the talbe row like this: ID Name Scoe 11 Tome 20 12 Jack 30 11 Tome 40 12 Jack 10 13 John 10 My query command like this: Select T1.Id,T1.Name,T2.mathfrom st T1right join(Select Id as Id2,Sum(Math) as Math from St group by id) T2on T1.id=t2.id2where t1.id = t2.id2 While the reuslt is : Id Name Score 11 Tom 60 11 Tom 60 12 Jake 40 12 Jack 40 13 John 10
I am wonder :the T1 gives a table with six rows, the T2 gives a table with three rows, and I use RIGHT JOIN to connect the two table,the result should be a table with only three rows.I tried INNER JOIN, the result is same. but why ? please help me !
Is it possible to pass entire where sentence to a store procedure?From app, I'll generate a where sentence like below:
where orderID = '123' and orderCidy='London'
I created a store procedure like below but got an error said that An expression of non-boolean type specified in a context where a condition is expected, near 'END'
CREATE PROCEDURE getorder @mywhere VARCHAR(100) AS BEGIN SET NOCOUNT ON; select * from order @mywhere END
Hi everyone!I am working with Delphi v7 and MS SQLServer.I am trying to insert data in a table with a SQL sentence. Some of thefields of my table are type char or varchar, and they can have nullvalues.What do i have to write in the SQL sentence to insert a null value inthose fields?I tried with '', an empty String, but it doesnt work, the tablesstores an empty String (logical :-)).In the SQLServer GUI you have to press CTRL + 0 to insert a NULLvalue, but how can i tell this to the SQLServer through a SQLSentence?Well, thank you very much.
I was recently told SSRS does not support the ability to underline a portion of text in a sentence. Does anyone know if this is true? Couldn't "text decoration" "underline" and "span" be used to underline the web address in the sample sentence below?
I have a illegal keyword table name as "Illegal_keyword_Master" where near about 2000 illegal keyword are stored... Now I have a big Sentence(bunch of words)...
If any illegal is keywords is found from "Illegal_keyword_Master" table in that sentence then the sentence is "banned" otherwise the sentence is "OK"....
I have an very long ntext field, made up of many sentences that I append a full stop to every one, I also strip out any line breaks within the text. However I get this error, when I look it up it comes up with "Failed to locate the ending boundary of a sentence."
I have the following SQL, which works but I think it can be done simplier. I seem to have to group it by multiple columns, but I am sure there must be a way of grouping the results by a single column. Any Ideas?
Code:
SELECT count(order_items.order_id) as treenum, orders.order_id, orders.order_date, orders.cust_order, orders.del_date, orders.confirmed, orders.del_addr FROM orders, order_items WHERE orders.order_id = order_items.order_id GROUP by orders.order_id, orders.order_date , orders.cust_order, orders.del_date, orders.confirmed, orders.del_addr ORDER BY orders.order_id DESC
I have four different transactions such as below and I do one insert and one update in each transaction and it seem it is slow and creates deadlock with the user interface.
These transactions are performed against the tables that users are accessing with another user interface. I have following two questions:
1. T2.TextField1 and TextField2 = @TextField2 are Ok, Nok fields so I did not put index since only two distinct values. Should I put indexes on these fields?
2. Can I make this transaction let user interface do its task in case accessing the same rows, I can start transaction again but I do not want users get disturbed? .
BEGIN TRANSACTION pTrans BEGIN INSERT INTO T1 (fields) SELECT (fields) FROM T2 INNER JOIN View1 ON T2.TrID = View1.MyTableID WHERE (T2.TextField1 = @TrType AND T2.TextField2 = @TextField2)
UPDATE T2 SET TextField2 = 'Ok', TextField2Date=@MyRunDateTime FROM T2 WHERE (TextField1 = @TrType AND TextField2 = @TextField2)
IF @@ERROR <> 0 BEGIN rollback transaction pTrans return(-1) END ELSE BEGIN commit transaction pTrans END END
I have a pretty good db server with four CPUs, it has not any other loads on it, but the following query takes 4ms to return. I use the syscolumns this way quite often, I am not sure why it takes it that long to return, any idea? select 'master',id,colid,name,xtype,length,xprec,xscale,status from [ablestatic].[dbo].syscolumns where id=(select id from [ablestatic].[dbo].sysobjects where name='link_data_ezregs')
hi, have a nice day to all , can some expert here point out what are the do and don't when we writing query ? ie:should we create many view as handler to get data? when should we use store procedure and trigger ? and so on
i seeking the way to improve my sql skill, can someone suggest some reference site or material or book as well ?
I wrote the following function a few years ago - before I learned about SQL's PATINDEX function. It might be possible to check for a valid email address syntax with a single PATINDEX string which could replace the entire body of hte function below.
Is anyone is interested in taking a crack at it?
Signed... lazy Sam
CREATE FUNCTION dbo.EmailIsValid (@Email varchar (100)) /* RETURN 1 if @Email contains a valid email address syntax, ELSE RETURN 0 */
RETURNS BIT AS BEGIN DECLARE @atpos int, @dotpos int
SET @Email = LTRIM(RTRIM(IsNull(@Email, ''))) -- remove leading and trailing blanks
IF LEN(@Email) = 0 RETURN(0) -- nothing to validate
SET @atpos = charindex('@',@Email) -- position of first (hopefully only) @
IF @atpos <= 1 OR @atpos = LEN(@Email) RETURN(0) -- @ is neither 1st or last or missing
IF CHARINDEX('@', @email, @atpos+1) > 0 RETURN(0) -- Two @s are illegal
IF CHARINDEX(' ',@Email) > 0 RETURN(0) -- Embedded blanks are illegal
SET @dotpos = CHARINDEX('.',REVERSE(@Email)) -- location (from rear) of last dot
IF (@dotpos < 3) or (@dotpos > 4) or (LEN(@Email) - @dotpos) < @atpos RETURN (0) -- dot / 2 or 3 char, after @
I have this SQL query that can take too long time, up to 1 minute if table contains over 1 million rows. And if the system is very active while executing this query it can cause more delays I guess.
select distinct 'CONV 1' as Conveyour, info as Error, (select top 1 substring(timecreated, 0, 7) from log b where a.info = b.info order by timecreated asc) as Date, (select count(*) from log b where b.info = a.info) as 'Times occured' from log a where loggroup = 'CSCNV' and logtype = 4
The table name is LOG, and I retrieve 4 columns: Conveyour, Error, Date and Times occured. The point of the subqueries is to count all distinct post and to retrieve the date of the first time the pst was logged. Also, a first and last date could be specified but is left out here.
Does anyone knows how I can improve this SQL query?
I have four different transactions such as below and I do one insert and one update in each transaction and it seem it is slow and creates deadlock with the user interface.
These transactions are performed against the tables that users are accessing with another user interface. I have following two questions:
1. T2.TextField1 and TextField2 = @TextField2 are Ok, Nok fields so I did not put index since only two distinct values. Should I put indexes on these fields?
2. Can I make this transaction let user interface do its task in case accessing the same rows, I can start transaction again but I do not want users get disturbed? .
BEGIN TRANSACTION pTrans BEGIN INSERT INTO T1 (fields) SELECT (fields) FROM T2 INNER JOIN View1 ON T2.TrID = View1.MyTableID WHERE (T2.TextField1 = @TrType AND T2.TextField2 = @TextField2)
UPDATE T2 SET TextField2 = 'Ok', TextField2Date=@MyRunDateTime FROM T2 WHERE (TextField1 = @TrType AND TextField2 = @TextField2)
IF @@ERROR <> 0 BEGIN rollback transaction pTrans return(-1) END ELSE BEGIN commit transaction pTrans END END
I have a quite big SQL query which would be nice to be used using UNION betweern two Select and Where clauses. I noticed that if both Select clauses have Where part between UNION other is ignored. How can I prevent this?
I found a article in StackOverflow saying that if UNION has e.g. two Selects with Where conditions other one will not work. [URL] ....
I have installed SQL Server 2014 and I tried to use tricks mentioned in StackOverflow's article but couldn't succeeded.
Any example how to write two Selects with own Where clauses and those Selects are joined with UNION?
hi in asp.net,i used sql server to store records. in a table has 1 million records,but when i update the record,it is very slowly. is "create index" helpful for "update" operation? i need help,thanks a lot.
Hi, We have a poorly performing SQL 2000 db. i have just defragged ( the HD, not indexes, these are done daily via SQL Agent) the data files of our server and have not found any improvement in response. I have now got into using SQL profiler to analyse the server performance. in the results that the trace is returning there are some huge (REALLY BIG) values for the duration and cpu values but these rows have no textdata value returned (ie it is null)
why is this? for these rows, the reads and writes columns are also high.
if these rows are what is taking the cpu's time then how can i identify what the server is doing to make any changes?
any thoughts on what other values i might trace or what action i can take to find the slow down cause?
in performance manager the processors (dual Xeons) are rarely dropping below 60%.
is there anyway to improve this statement. make it short? any input will be appreciated.
update TempAddressParsingTable set ad_unit = case
when right(rtrim(ad_str1),3) like 'apt' or right(rtrim(ad_str1),3) like 'lot' and substring(reverse(ad_str1), 4,1) in ('', ',', '.') then right(ad_str1,3)
when right(rtrim(ad_str1),4) like 'unit' or right(rtrim(ad_str1),4) like 'apt%' or right(rtrim(ad_str1),4) like 'lot%' and substring(reverse(ad_str1), 5,1) in ('', ',', '.') then right(ad_str1,4)
when right(rtrim(ad_str1),5) like 'unit%' or right(rtrim(ad_str1),5) like 'apt%%' or right(rtrim(ad_str1),5) like 'lot%%' and substring(reverse(ad_str1), 6,1) in ('', ',', '.') then right(ad_str1,5)
when right(rtrim(ad_str1),6) like 'unit%%' or right(rtrim(ad_str1),6) like 'apt%%%' or right(rtrim(ad_str1),6) like 'lot%%%' and substring(reverse(ad_str1), 7,1) in ('', ',', '.') then right(ad_str1,6)
when right(rtrim(ad_str1),7) like 'unit%%%' or right(rtrim(ad_str1),7) like 'apt%%%%' or right(rtrim(ad_str1),7) like 'lot%%%%' and substring(reverse(ad_str1), 8,1) in ('', ',', '.') then right(ad_str1,7)
when right(rtrim(ad_str1),8) like 'unit%%%%' or right(rtrim(ad_str1),8) like 'apt%%%%%' and substring(reverse(ad_str1), 9,1) in ('', ',', '.') then right(ad_str1,8)
when right(rtrim(ad_str1),9) like 'unit%%%%%' or right(rtrim(ad_str1),9) like 'apt%%%%%%' and substring(reverse(ad_str1), 10,1) in ('', ',', '.') then right(ad_str1,9)
when right(rtrim(ad_str1), 2) like '#%' and substring(reverse(ad_str1), 3, 1) in ('', ',', '.') then right(ad_str1, 2) when right(rtrim(ad_str1), 3) like '#%%' and substring(reverse(ad_str1), 4, 1) in ('', ',', '.') then right(ad_str1, 3) when right(rtrim(ad_str1), 4) like '#%%%' and substring(reverse(ad_str1), 5, 1) in ('', ',', '.') then right(ad_str1, 4) when right(rtrim(ad_str1), 5) like '#%%%%' and substring(reverse(ad_str1), 6, 1) in ('', ',', '.') then right(ad_str1, 5) else null end