Search Results

Search found 1214 results on 49 pages for 'tomaz tsql'.

Page 17/49 | < Previous Page | 13 14 15 16 17 18 19 20 21 22 23 24  | Next Page >

  • Efficiency of checking for null varbinary(max) column ?

    - by Moe Sisko
    Using SQL Server 2008. Example table : CREATE table dbo.blobtest (id int primary key not null, name nvarchar(200) not null, data varbinary(max) null) Example query : select id, name, cast((case when data is null then 0 else 1 end) as bit) as DataExists from dbo.blobtest Now, the query needs to return a "DataExists" column, that returns 0 if the blob is null, else 1. This all works fine, but I'm wondering how efficient it is. i.e. does SQL server need to read in the whole blob to its memory, or is there some optimization so that it just does enough reads to figure out if the blob is null or not ? (FWIW, the sp_tableoption "large value types out of row" option is set to OFF for this example).

    Read the article

  • Does query plan optimizer works well with joined/filtered table-valued functions?

    - by smoothdeveloper
    In SQLSERVER 2005, I'm using table-valued function as a convenient way to perform arbitrary aggregation on subset data from large table (passing date range or such parameters). I'm using theses inside larger queries as joined computations and I'm wondering if the query plan optimizer work well with them in every condition or if I'm better to unnest such computation in my larger queries. Does query plan optimizer unnest table-valued functions if it make sense? If it doesn't, what do you recommend to avoid code duplication that would occur by manually unnesting them? If it does, how do you identify that from the execution plan? code sample: create table dbo.customers ( [key] uniqueidentifier , constraint pk_dbo_customers primary key ([key]) ) go /* assume large amount of data */ create table dbo.point_of_sales ( [key] uniqueidentifier , customer_key uniqueidentifier , constraint pk_dbo_point_of_sales primary key ([key]) ) go create table dbo.product_ranges ( [key] uniqueidentifier , constraint pk_dbo_product_ranges primary key ([key]) ) go create table dbo.products ( [key] uniqueidentifier , product_range_key uniqueidentifier , release_date datetime , constraint pk_dbo_products primary key ([key]) , constraint fk_dbo_products_product_range_key foreign key (product_range_key) references dbo.product_ranges ([key]) ) go . /* assume large amount of data */ create table dbo.sales_history ( [key] uniqueidentifier , product_key uniqueidentifier , point_of_sale_key uniqueidentifier , accounting_date datetime , amount money , quantity int , constraint pk_dbo_sales_history primary key ([key]) , constraint fk_dbo_sales_history_product_key foreign key (product_key) references dbo.products ([key]) , constraint fk_dbo_sales_history_point_of_sale_key foreign key (point_of_sale_key) references dbo.point_of_sales ([key]) ) go create function dbo.f_sales_history_..snip.._date_range ( @accountingdatelowerbound datetime, @accountingdateupperbound datetime ) returns table as return ( select pos.customer_key , sh.product_key , sum(sh.amount) amount , sum(sh.quantity) quantity from dbo.point_of_sales pos inner join dbo.sales_history sh on sh.point_of_sale_key = pos.[key] where sh.accounting_date between @accountingdatelowerbound and @accountingdateupperbound group by pos.customer_key , sh.product_key ) go -- TODO: insert some data -- this is a table containing a selection of product ranges declare @selectedproductranges table([key] uniqueidentifier) -- this is a table containing a selection of customers declare @selectedcustomers table([key] uniqueidentifier) declare @low datetime , @up datetime -- TODO: set top query parameters . select saleshistory.customer_key , saleshistory.product_key , saleshistory.amount , saleshistory.quantity from dbo.products p inner join @selectedproductranges productrangeselection on p.product_range_key = productrangeselection.[key] inner join @selectedcustomers customerselection on 1 = 1 inner join dbo.f_sales_history_..snip.._date_range(@low, @up) saleshistory on saleshistory.product_key = p.[key] and saleshistory.customer_key = customerselection.[key] I hope the sample makes sense. Much thanks for your help!

    Read the article

  • Table-level diff and sync procedure for T-SQL

    - by Ville Koskinen
    I'm interested in T-SQL source code for synchronizing a table (or perhaps a subset of it) with data from another similar table. The two tables could contain any variables, for example I could have base table source table ========== ============ id val id val ---------- ------------ 0 1 0 3 1 2 1 2 2 3 3 4 or base table source table =================== ================== key val1 val2 key val1 val2 ------------------- ------------------ A 1 0 A 1 1 B 2 1 C 2 2 C 3 3 E 4 0 or any two tables containing similar columns with similar names. I'd like to be able to check that the two tables have matching columns: the source table has exactly the same columns as the base table and the datatypes match make a diff from the base table to the source table do the necessary updates, deletes and inserts to change the data in the base table to correspond the source table optionally limit the diff to a subset of the base table, preferrably with a stored procedure. Has anyone written a stored proc for this or could you point to a source?

    Read the article

  • sybase - fails to use index unless string is hard-coded

    - by Garrett
    I'm using Sybase 12.5.3 (ASE); I'm new to Sybase though I've worked with MSSQL pretty extensively. I'm running into a scenario where a stored procedure is really very slow. I've traced the issue to a single SELECT stmt for a relatively large table. Modifying that statement dramatically improves the performance of the procedure (and reverting it drastically slows it down; i.e., the SELECT stmt is definitely the culprit). -- Sybase optimizes and uses multi-column index... fast!<br> SELECT ID,status,dateTime FROM myTable WHERE status in ('NEW','SENT') ORDER BY ID -- Sybase does not use index and does very slow table scan<br> SELECT ID,status,dateTime FROM myTable WHERE status in (select status from allowableStatusValues) ORDER BY ID The code above is an adapted/simplified version of the actual code. Note that I've already tried recompiling the procedure, updating statistics, etc. I have no idea why Sybase ASE would choose an index only when strings are hard-coded and choose a table scan when choosing from another table. Someone please give me a clue, and thank you in advance.

    Read the article

  • T-SQL query to check number of existences

    - by abatishchev
    I have next approximate tables structure: accounts: ID INT, owner_id INT, currency_id TINYINT related to clients: ID INT and currency_types: ID TINYINT, name NVARCHAR(25) I need to write a stored procedure to check existence of accounts with specific currency and all others, i.e. client can have accounts in specific currency, some other currencies and both. I have already written this query: SELECT ISNULL(( SELECT 1 WHERE EXISTS ( SELECT 1 FROM [accounts] AS A, [currency_types] AS CT WHERE A.[owner_id] = @client -- sp param AND A.[currency_id] = CT.[ID] AND CT.[name] = N'Ruble' )), 0) AS [ruble], ISNULL(( SELECT 1 WHERE EXISTS ( SELECT A.[ID] FROM [accounts] AS A, [currency_types] AS CT WHERE A.[owner_id] = @client AND A.[currency_id] = CT.[ID] AND CT.[name] != N'Ruble' )), 0) AS [foreign] Is it possible to optimize it? I'm new to (T)SQL, so thanks in advance!

    Read the article

  • Select dynamic string has a different value when referenced in Where clause

    - by David
    I dynamically select a string built using another string. So, if string1='David Banner', then MyDynamicString should be 'DBanne' Select ... , Left( left((select top 1 strval from dbo.SPLIT(string1,' ')) //first word ,1) //first character + (select top 1 strval from dbo.SPLIT(string1,' ') //second word where strval not in (select top 1 strval from dbo.SPLIT(string1,' '))) ,6) //1st character of 1st word, followed by up to 5 characters of second word [MyDynamicString] ,... From table1 Join table2 on table1pkey=table2fkey Where MyDynamicString <> table2.someotherfield I know table2.someotherfield is not equal to the dynamic string. However, when I replace MyDynamicString in the Where clause with the full left(left(etc.. function, it works as expected. Can I not reference this string later in the query? Do I have to build it using the left(left(etc.. function each time in the where clause?

    Read the article

  • Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?

    - by ctrlShiftBryan
    Using MSSQL2005, Can I truncate a table with a foreign key constraint if I first truncate the child table(the table with the primary key of the FK relationship)? I know I can use a DELETE without a where clause and then RESEED the identity OR Remove the FK, truncate and recreate but I thought as long as you truncate the child table you'll be OK however I'm getting a "Cannot truncate table 'TableName' because it is being referenced by a FOREIGN KEY constraint." error.

    Read the article

  • sort items based on their appears count

    - by ANIL MANE
    Hello Experts, I have data like this d b c a d c b a b c a c a d c if you analyse, you will find the appearance of each element as follows a: 4 b: 3 c: 5 d: 2 According to appearance my sorted elements would be c,a,b,d and final output should be c b d a d c b a b c a c a d c Any clue, how we can achieve this using sql query ?

    Read the article

  • nested insert exec work around

    - by stackoverflowuser
    i have 2 stored procedures usp_SP1 and usp_SP2. Both of them make use of insert into #tt exec sp_somesp. I wanted to created a 3rd stored procedure which will decide which stored proc to call. something like create proc usp_Decision ( @value int ) as begin if (@value = 1) exec usp_SP1 -- this proc already has insert into #tt exec usp_somestoredproc else exec usp_SP2 -- this proc too has insert into #tt exec usp_somestoredproc end Later realized I needed some structure defined for the return value from usp_Decision so that i can populate the SSRS dataset field. So here is what i tried: within usp_Decision created a temp table and tried to do "insert into #tt exec usp_SP1". Didn't work out. error "insert exec cannot be nested" within usp_Decision tried passing table variable to each of the stored proc and update the table within the stored procs and do "select * from ". That didnt work out as well. Table variable passed as parameter cannot be modified within the stored proc. Pls. suggest what can de done?

    Read the article

  • SQL IF ELSE BEGIN END

    - by Swami
    If there are no begin and end statements in sql, the next statement is the only one that gets executed if the if condition is true...in the case below, is there anyway the insert statement will also get executed if the if condition is true? IF (a > 1) SET @b = 1 + 2 INSERT INTO #F (a, b, c) VALUES (1, 2, 3)

    Read the article

  • SQL Select Permissions

    - by Brandi
    I have a database that I need to connect to and select from. I have an SQL Login, let's call it myusername. When I use the following, no SELECT permission shows up: SELECT * FROM fn_my_permissions ('dbo.mytable', 'OBJECT') GO Several times I tried things like: USE mydatabase GO GRANT SELECT TO myusername GO GRANT SELECT ON DATABASE::mydatabase TO myusername GO GRANT SELECT ON mytable TO myusername GO It says the queries execute successfully, but there is never any difference in the first query. What simple thing am I missing to grant database level select permissions. As a note, I made double sure it was the correct user, correct database, and I have already tried granting table level select permissions. So far I keep getting the error: SELECT permission denied on object 'mytable', database 'mydatabase', schema 'dbo'. Any ideas what I'm missing? Thanks in advance.

    Read the article

  • CHECK/NOCHECK for Sql Compact Edition

    - by Ryan H
    I am attempting to wipe and repopulate test data on SQL CE. I am getting an error due to FK constraints existing. Typically in Sql2005 I would ALTER TABLE [tablename] CHECK/NOCHECK CONSTRAINT ALL to enable/disable all constraints. From what I could find in my searching, it seems that this might not be supported in CE. Is that true? If so, is there an alternative?

    Read the article

  • What operation type invoked a trigger in SQL Server 2008?

    - by Neil Moss
    I'm contemplating a single SQL trigger to handle INSERT, UPDATE and DELETE operations as part of an auditing process. Is there any statement, function or @@ variable I can interrogate to find out which operation type launched the trigger? I've seen the following pattern: declare @type char(1) if exists (select * from inserted) if exists (select * from deleted) select @Type = 'U' else select @Type = 'I' else select @Type = 'D' but is there anything else a little more direct or explicit? Thanks, Neil.

    Read the article

  • SQL Get Top 10 records by date

    - by Pselus
    I have a table full of bugs. The BugTitle is the page erroring and I also capture the error line. I would like to build an SQL Query that selects the top 10 bugs based on bugtitle and error line. I have this query: SELECT COUNT(BugTitle) AS BugCount, BugTitle, ErrLine FROM Bugs WHERE BugDate >= DateAdd(Day, -30, DateDiff(Day, 0, GetDate())) GROUP BY BugTitle, ErrLine ORDER BY BugCount, ErrLine DESC But I'm not sure if it's correct. I'm pretty sure that my test data only has 1 bug that happens on the same line but that's not showing up with this query. Can anyone help?

    Read the article

  • Select Statement to show missing records (Easy Question)

    - by Gerhard Weiss
    I need some T-SQL that will show missing records. Here is some sample data: Emp 1 01/01/2010 02/01/2010 04/01/2010 06/01/2010 Emp 2 02/01/2010 04/01/2010 05/01/2010 etc... I need to know Emp 1 is missing 03/01/2010 05/01/2010 Emp 2 is missing 01/01/2010 03/01/2010 06/01/2010 The range to check will start with todays date and go back 6 months. In this example, lets say today's date is 06/12/2010 so the range is going to be 01/01/2010 thru 06/01/2010. The day is always going to be the 1st in the data. Thanks a bunch. :) Gerhard Weiss Secretary of Great Lakes Area .NET Users Group GANG Upcoming Meetings | GANG LinkedIn Group

    Read the article

  • Why decimal behave differently?

    - by haansi
    Hello, I am doing this small exercise. declare @No decimal(38,5); set @No=12345678910111213.14151; select @No*1000/1000,@No/1000*1000,@No; Results are: 12345678910111213.141510 12345678910111213.141000 12345678910111213.14151 Why are the results of first 2 selects different when mathematically it should be same?

    Read the article

  • SELECT TOP N With Two Variables

    - by Ricardo Deano
    Hello all. It's Tuesday morning and I am being thick as (I'm blaming my daughter waking up early this morning!) I have the following example in a SQL table Cust Group Sales A 1 15 A 1 10 A 1 5 A 2 15 A 2 10 A 2 5 B 1 15 B 1 10 B 1 5 B 2 15 B 2 10 B 2 5 What I would like to show is the top 2 products per customer, per group sorted descending by Sales i.e. Cust Group Sales A 1 15 A 1 10 A 2 15 A 2 10 B 1 15 B 1 10 B 2 15 B 2 10 I'm assuming I need to declare two variables, Cust and Group, I'm just not sure how to complete this in one fell swoop. Apologies for the thick question...no excuse. Thanks for any help.

    Read the article

  • How to add an additional column in a table by comparing the values in a different column

    - by S-Man
    I have a table with the following records id name city 1 aaa NY 2 bbb NY 3 ccc LA 4 ddd LA 5 eee NY I want the table with an additional column by comparing the 'city' column. The values in the col4 should have '1' for every unique value in 'city' column and '0' for the repeating values in 'city' column. id name city col4 1 aaa NY 1 2 bbb NY 0 3 ccc LA 1 4 ddd LA 0 5 eee NY 0 I hope to get some help. Thanks

    Read the article

< Previous Page | 13 14 15 16 17 18 19 20 21 22 23 24  | Next Page >