Search Results

Search found 36773 results on 1471 pages for 'sql statement syntax'.

Page 127/1471 | < Previous Page | 123 124 125 126 127 128 129 130 131 132 133 134  | Next Page >

  • sql server procedure optimization

    - by stackoverflow
    SQl Server 2005: Option: 1 CREATE TABLE #test (customerid, orderdate, field1 INT, field2 INT, field3 INT) CREATE UNIQUE CLUSTERED INDEX Idx1 ON #test(customerid) CREATE INDEX Idx2 ON #test(field1 DESC) CREATE INDEX Idx3 ON #test(field2 DESC) CREATE INDEX Idx4 ON #test(field3 DESC) INSERT INTO #test (customerid, orderdate, field1 INT, field2 INT, field3 INT) SELECT customerid, orderdate, field1, field2, field3 FROM ATABLERETURNING4000000ROWS compared to Option: 2 CREATE TABLE #test (customerid, orderdate, field1 INT, field2 INT, field3 INT) INSERT INTO #test (customerid, orderdate, field1 INT, field2 INT, field3 INT) SELECT customerid, orderdate, field1, field2, field3 FROM ATABLERETURNING4000000ROWS CREATE UNIQUE CLUSTERED INDEX Idx1 ON #test(customerid) CREATE INDEX Idx2 ON #test(field1 DESC) CREATE INDEX Idx3 ON #test(field2 DESC) CREATE INDEX Idx4 ON #test(field3 DESC) When we use the second option it runs close to 50% faster. Why is this?

    Read the article

  • SELECT..CASE - Refactor T-SQL

    - by Nev_Rahd
    Hello Can I refactor the below SQL CASE statements into single for each case ? SELECT CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.FAMILY_NAME ELSE NULL END AS [LEGAL_FAMILY_NAME], CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.GIVEN_NAME ELSE NULL END AS [LEGAL_GIVEN_NAME], CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.MIDDLE_NAMES ELSE NULL END AS [LEGAL_MIDDLE_NAMES], CASE RDV.DOMAIN_CODE WHEN 'L' THEN CN.NAME_TITLE ELSE NULL END AS [LEGAL_NAME_TITLE], CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.FAMILY_NAME ELSE NULL END AS [PREFERRED_FAMILY_NAME], CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.GIVEN_NAME ELSE NULL END AS [PREFERRED_GIVEN_NAME], CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.MIDDLE_NAMES ELSE NULL END AS [PREFERRED_MIDDLE_NAMES], CASE RDV.DOMAIN_CODE WHEN 'P' THEN CN.NAME_TITLE ELSE NULL END AS [PREFERRED_NAME_TITLE] FROM dbo.CLIENT_NAME CN JOIN dbo.REFERENCE_DOMAIN_VALUE RDV ON CN.NAME_TYPE_CODE = RDV.DOMAIN_CODE AND RDV.REFERENCE_DOMAIN_ID = '7966'

    Read the article

  • SSMS Ctrl+Tab Page Thumbnail Preview Add-on?

    - by Catto
    Hey Now, Is there a SQL Server Management studio add-on/plug-in that will enhance the tab switching dialog box when Ctrl+Tab is used to switch tabs to provide a thumbnail image of the tab? When we use Ctrl+Tab to switch tabs we see the list of tabs open. When we switch tabs in Visual Studio we get a small thumb nail preview screen of the tab along with the tab names. It would be great if there was an add-on that we could install to provide an image in addition to the file names. Thx, Catto

    Read the article

  • Very difficult SQL query

    - by db666
    For the following table definitions: Name Null? Type Comments ------------------------------- -------- ---- ------------------------------------ ENUM NOT NULL NUMBER(4) ENUM should not exceed a length of 4. ENAME CHAR(15) ADDRESS CHAR(25) ADDRESS should not exceed 25 characters. SALARY NUMBER(5) OFFICE CHAR(4) DNUM NOT NULL NUMBER(4) Department which this employee belongs to department Name Null? Type Comments ------------------------------- -------- ---- ------------------------------------- DNUM NOT NULL NUMBER(4) DMGR NOT NULL NUMBER(4) Department manager DNAME NOT NULL CHAR(15) project Name Null? Type Comments ------------------------------- -------- ---- ------------------------------------- PNUM NOT NULL NUMBER(4) PMGR NOT NULL NUMBER(4) Project manager PTITLE NOT NULL CHAR(15) emp_proj Name Null? Type ------------------------------- -------- ---- PNUM NOT NULL NUMBER(4) ENUM NOT NULL NUMBER(4) I have to write SQL query which will find the names of employees who do not share an office but work on the same project, and have different salaries... I've spent last three days trying to figure out something, but no idea as far. I will appreciate any advice.

    Read the article

  • PostgreSQL function to iterate through/act on many rows with state

    - by Claudiu
    I have a database with columns looking like: session | order | atype | amt --------+-------+-------+----- 1 | 0 | ADD | 10 1 | 1 | ADD | 20 1 | 2 | SET | 35 1 | 3 | ADD | 10 2 | 0 | SET | 30 2 | 1 | ADD | 20 2 | 2 | SET | 55 It represents actions happening. Each session starts at 0. ADD adds an amount, while SET sets it. I want a function to return the end value of a session, e.g. SELECT session_val(1); --returns 45 SELECT session_val(2); --returns 55 Is it possible to write such a function/query? I don't know how to do any iteration-like things with SQL, or if it's possible at all.

    Read the article

  • Execute Statements in a Transaction - Sql Server 2005

    - by Shrewd Demon
    hi, i need to update a database where some of the table have changed (columns have been added). I want to perform this action in a proper transaction. If the code executes without any problem then i will commit the changes otherwise i will rollback the database back to its original state. I want to do something like this: BEGIN TRANSACTION ...Execute some sql statements here COMMIT TRANSACTION (When every thing goes well) ROLLBACK TRANSACTION (When something goes wrong) Please tell me what is the best way to do this i know there is a @@TranCount variable but dont know its exact purpose. Thanks.

    Read the article

  • How to do insert one row from one table to another table(what has less columns)?

    - by chobo2
    Hi I am trying to find rows that are not in one table and insert them into another. The table I am trying to insert into has less columns then the other one. These columns are null but it would be cool if I could hardcode a value before I do the insert. But I am having so much trouble with just trying to get it to insert. I have something like this SELECT p.ProductId, p.ProductName INTO SomeTable FROM Product as p WHERE p.ProductName != 'iPad' I will get a error like this though Msg 4104, Level 16, State 1, Line 1 The multi-part identifier "p.ProductId" could not be bound. I am not sure what I am doing wrong. I copied and pasted the names in so I don't think it is a spelling mistake. I am using ms sql 2005 express.

    Read the article

  • Getting Top (starting no , Ending no) rows from sqlserver

    - by Krishna Thota
    I'm using Grid view in my asp.net page and I'm fetching data from SQL Server and placing in my grid view. Now my problem is, I'm using paging and placing 50 rows per page in Grid view. I would like to fetch top 50 rows from database in the start up and bind to the Grid View and when i click on next page, then again go to the database and fetch Second Top 50 rows and bind to GV. and this process hasto continue until last row is fetched from Database. Can you tell me How to write Query in achieving this??

    Read the article

  • SQL Server 2008 Delete Records from Self-Referencing Table in correct order

    - by KTrace
    I need to delete a sub set of records from a self-referencing table in SQL Server 2008. I am trying to do the following but it is does not like the order by. WITH SelfReferencingTable (ID, depth) AS ( SELECT id, 0 as [depth] FROM dbo.Table WHERE parentItemID IS NULL AND [t].ColumnA = '123' UNION ALL SELECT [t].ID, [srt].[depth] + 1 FROM dbo.Table t INNER JOIN SelfReferencingTable srt ON [t].parentItemID = [srt].id WHERE [t].ColumnA = '123' ) DELETE y FROM dbo.Table y JOIN SelfReferencingTable x on x.ID = y.id ORDER BY x.depth DESC Any ideas why this isn't working?

    Read the article

  • SQL 2008 table locked - can't work out why

    - by Mr. Flibble
    I have two databases on one SQL 2008 server. Database 1 seems to be causing a lock on a table on database 2. There no queries are running on database 1 that should affect database 2. Is this normal behaviour? When I view the running queries with this command SELECT sqltext.TEXT, req.session_id, req.status, req.command, req.cpu_time, req.total_elapsed_time/1000 [seconds] FROM sys.dm_exec_requests req CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext it tells me as much, and says that the command on database 2 is suspended. I'm at a bit of a loss. What sort of things should I look at to work out why the table in database 2 is locked?

    Read the article

  • read contents of a sql table into a data grid view

    - by syedsaleemss
    Im using c# .net windows application form. i have created a database named "resources" in SQL server 2005. In that i have a table named "resourcetable" which has two columns. now i need to read the data in the table into a datagridview. I can do this by using the connection string and querry by giving the table name. But my problem is i have a button and if i click on that button, an open dialog box should open and i should be able to select a database amomng many databases and in that selected database i have to select one table amomg many tables. i.e select a table ofa particular database. and the contents of that table are to be displayed in a datagridview.

    Read the article

  • Performance improvement to a big if clause in SQL Server function

    - by Miles D
    I am maintaining a function in SQL Server 2005, that based on an integer input parameter needs to call different functions e.g. IF @rule_id = 1 -- execute function 1 ELSE IF @rule_id = 2 -- execute function 2 ELSE IF @rule_id = 3 ... etc The problem is that there are a fair few rules (about 100), and although the above is fairly readable, its performance isn't great. At the moment it's implemented as a series of IF's that do a binary-chop, which is much faster, but becomes fairly unpleasant to read and maintain. Any alternative ideas for something that performs well and is fairly maintainable?

    Read the article

  • Should I rebuild table indexes after a SQL Server 2000 to 2005 database migration

    - by Joe T
    I'm tasked with doing a SQL Server 2000 to 2005 migration. I will be doing a side-by-side migration. After restoring from a backup I plan to do the following: ALTER DATABASE <database_name> SET COMPATIBILITY_LEVEL = 90; DBCC CHECKDB(<database_name>) WITH NO_INFOMSGS DBCC UPDATEUSAGE(<database_name>) WITH NO_INFOMSGS exec sp_updatestats ‘resample’ Should I rebuild table indexes before using DBCC UPDATEUSAGE and sp_updatestats? Have I missed anything obvious that should be executed after a migration? All help would be warmly up-voted. Thanks

    Read the article

  • Getting age in years in a SQL query

    - by Earlz
    Hello I've been tasked with doing a few queries on a large SQL Server 2000 database. The query I'm having trouble with is "find the number of people between ages 20 and 40" How would I do this? My previous query to get a count of everyone looks like this: select count(rid) from people where ... (with the ... being irrelevant conditions). I've googled some but the only thing I've found for calculating age is so large that I don't see how to embed it into a query, or it is a stored procedure which I do not have the permissions to create. Can someone help me with this?

    Read the article

  • Working by group by for grouping data into string format

    - by Shantanu Gupta
    I have a table that contains some data given below. It uses a tree like structure i.e. Department SubD1, SubD2 ..... PreSubD1, PreSubD1... PreSubD2, PreSubD2... pk_map_id preferences ImmediateParent Department_Id -------------------- -------------------- -------------------- -------------------- 20 14 5 1 21 15 5 1 22 16 6 1 23 9 4 2 24 4 3 2 25 24 20 2 26 25 20 2 27 23 13 2 I want to group my records on behalf of department then immediate parent then preferences each seperated by ',' i.e. department Immediate Parent preferences 1 5,6 14,15,16 2 4,3,20,13 9,4,24,25,23 and this table also Immediate parent preferences 5 14,15 6 16 4 9 3 4 20 24,25 13 13 In actual scenario all these are my ids which are to be replaced by their string fields. I am using sql server 2k5

    Read the article

  • SQL Drop Index on different Database

    - by Jim
    While trying to optimize SQL scripts, I was recommended to add indexes. What is the easiest way to specify what Database the index should be on? IF EXISTS (SELECT * FROM sysindexes WHERE NAME = 'idx_TableA') DROP INDEX TableA.idx_TableA IF EXISTS (SELECT * FROM sysindexes WHERE NAME = 'idx_TableB') DROP INDEX TableB.idx_TableB In the code aboce, TableA is in DB-A, and TableB is in DB-B. I get the following error when I change DROP INDEX TableA.idx_TableA to DROP INDEX DB-A.dbo.TableA.idx_TableA Msg 166, Level 15, State 1, Line 2 'DROP INDEX' does not allow specifying the database name as a prefix to the object name. Any thoughts are appreciated.

    Read the article

  • SQL Server GROUP BY troubles!

    - by Lucas311
    I'm getting a frustrating error in one of my SQL Server 2008 queries. It parses fine, but crashes when I try to execute. The error I get is the following: Msg 8120, Level 16, State 1, Line 4 Column 'customertraffic_return.company' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. SELECT * FROM (SELECT ctr.sp_id AS spid, Substring(ctr.company, 1, 20) AS company, cci.email_address AS tech_email, CASE WHEN rating IS NULL THEN 'unknown' ELSE rating END AS rating FROM customer_contactinfo cci INNER JOIN customertraffic_return ctr ON ctr.sp_id = cci.sp_id WHERE cci.email_address <> '' AND cci.email_address NOT LIKE '%hotmail%' AND cci.email_address IS NOT NULL AND ( region LIKE 'Europe%' OR region LIKE 'Asia%' ) AND SERVICE IN ( '1', '2' ) AND ( rating IN ( 'Premiere', 'Standard', 'unknown' ) OR rating IS NULL ) AND msgcount >= 5000 GROUP BY ctr.sp_id, cci.email_address) AS a WHERE spid NOT IN (SELECT spid FROM customer_exclude) GROUP BY spid, tech_email

    Read the article

  • String update in SQL Server

    - by Thiyaneshwaran S
    Currently I have varchar field. The delimiter is "$P$P$". The delimiter will appear at least once and at most twice in the varchar data. Eg. Sample Heading$P$P$Sample description$P$P$Sample conclusion Sample Heading$P$P$Sample Description If the delimiter appears twice, I need to insert a text before the second occurance of the delimiter. Eg: Sample Heading$P$P$Sample DescriptionINSERT TEXT HERE$P$P$Sample Conclusion If the delimiter occurs only once, then I need to insert a text at the end of the field. Eg: Sample Heading$P$P$Sample DescriptionAPPEND TEXT HERE How this can be done in SQL query?

    Read the article

  • MySQL "IS IN" equivalent?

    - by nute
    A while ago I worked on a MS-SQL project and I remember a "IS IN" thing. I tried it on a MySQL project and it did not work. Is there an equivalent? Workaround? Here is the full query I am trying to run: SELECT * FROM product_product, product_viewhistory, product_xref WHERE ( (product_viewhistory.productId = product_xref.product_id_1 AND product_xref.product_id_2 = product_product.id) OR (product_viewhistory.productId = product_xref.product_id_2 AND product_xref.product_id_1 = product_product.id) ) AND product_product.id IS IN (SELECT DISTINCT pvh.productId FROM product_viewhistory AS pvh WHERE pvh.cookieId = :cookieId ORDER BY pvh.viewTime DESC LIMIT 10) AND product_viewhistory.cookieId = :cookieId AND product_product.outofstock='N' ORDER BY product_xref.hits DESC LIMIT 10 It's pretty big ... but the part I am interested in is: AND product_product.id IS IN (SELECT DISTINCT pvh.productId FROM product_viewhistory AS pvh WHERE pvh.cookieId = :cookieId ORDER BY pvh.viewTime DESC LIMIT 10) Which basically says I want the products to be in the "top 10" of that sub-query. How would you achieve that with MySQL (while trying to be efficient)?

    Read the article

  • SQL Server Express: Failed to add primary key after creating a database table

    - by Syd
    Scenario: I have created a database table. And when I tried to add a primary key (using the SQL Server Management Studiio) to the table, it failed with an error message "Saving changes is not permitted. The changes you have made requrie the following tables to be dropped and re-created. You have either made changes to a table that can't be recreated or enabled the option Prevent savign changes that require the table to be re-created." The environment is Windows 7. I have earlier installed it on a Windows XP SP2 and it worked. And yes, my table is empty (freshly created). My current workaround is to run the "Drop and Create" script. The other alternative is to create the primary key before I save the table. Is this a bug or a limitation with the Express version?

    Read the article

  • Easy plugin or procedure for sqlserver Management Studio to script row inserts.

    - by Patrick Karcher
    I've never been able to find a good script or plugin for sql server Management Studio (2005 and or 2008) for a very common scripting need: specifying a few/all rows in a table and scripting their insert. You can guess my story: I've got some configuration data in my dev db and I need to script it for deployment to UAT and then production. I've found a few cludgy systems in the past, that were more trouble than they were worth. I need something free and unobtrusive. Once I find it I'll share it with the other 20 developers in my shop who are annoyed by this. Aren't we all annoyed by this by the way? What is the best, easiest, free, way to specify a few/all rows in a table and get a script their insert?

    Read the article

  • SQL compare entire rows

    - by zmaster
    In SQL server 2008 I have some huge tables (200-300+ cols). Every day we run a batch job generating a new table with timestamp appended to the name of the table. The the tables have no PK. I would like a generic way to compare 2 rows from two tables. Showing which cols having different values is sufficient, but showing the values would be perfect. Thanks a lot Thanks for the answers. I ended up writing my own C# tool to do the job - as Im not allowed to install 3rd party software in my company.

    Read the article

  • sql - duplicates

    - by Sebastjan
    Hey guys I'm putting data from website (json) to sql base. In db i have these rows. ID | PostId | Name | Message Id is auto-increment primary key. PostId also has a unique values. Name and Message are nothing special. When I run my script / click on the button in form / ... , the program saves all the values into database (lets say there are 25). Next time I'm going to press the button there will be added 25 more records (all duplicates), and so on... Is there a way that the program can check through 'PostIds' if the value already exists before adding it to the db? Thanks

    Read the article

  • Finding records within a 5 min time interval in SQL

    - by Mellonjollie
    I have a table with over 100,000 rows that contain the following columns: ID, Time, and Boolean. The time column tracks time down to the second. I need a query that will find all instances of Boolean = 1 for every 5 minute interval of time from the start of the table to the end, then group the count by time interval. The table represents 4 hours of data, so I should get 48 rows of results. I'm using MS SQL Server. I've tried a few approaches, but the time interval logic is giving me a hard time.

    Read the article

  • Select rows with same column A but different column B

    - by Flip Booth
    ID Zip Room ----------- ---------- ------ 317 94087 S105 318 94087 L603 1739 94404-1801 L603 1823 94401-2129 L603 1824 94401-2129 L603 2135 94404-1801 L603 2268 95136-1459 S604 2269 95136-1459 S604 3704 92673-6417 L402 4479 93454-9670 L402 4480 93454-9670 L402 4782 92395-4681 L402 4783 92395-4681 L402 4852 92886-4411 L402 4853 92886-4411 L402 4959 92673-6417 L402 5153 91773-4028 L402 5202 91773-4028 L402 5211 91765-2959 L402 5212 91765-2959 L402 5388 92336-0605 L402 5392 92336-0605 L402 5727 92870 L402 5728 92870 L402 5831 92557 L402 5916 92557 L402 How do I select ID's that has THE SAME zip but different Room ? In the table above, I want the result to be: ID Zip Room ----------- ---------- ------ 317 94087 S105 318 94087 L603 Using SQL Server 2008

    Read the article

< Previous Page | 123 124 125 126 127 128 129 130 131 132 133 134  | Next Page >