sql

SQL Search with parameter that could be date or could be string

Hi there. I have a search box where you can enter plain text. The keywords are separated by spaces. I check each keyword if it fits the format for a date and store 'all keywords' in one array and 'date keywords' in another. All the 'date keywords' are also in the 'all keywords' array, as I can not tell if its 'only' a date or maybe also ...

Update trigger insert Null

Hi! I am trying to create trigger on SQL Server 2008. I want that if i update field in tabele log that the new value update field in another table Doc. This is the code for trigger: Create TRIGGER dbo.DocSt ON dbo.log AFTER UPDATE IF (SELECT COUNT(*) FROM inserted) > 0 BEGIN IF (SELECT COUNT(*) FROM deleted) > 0 ...

Foreign key referencing a 2 columns primary key in SQL Server

This question is pretty much similar to this one, but for SQL Server 2005 : I have 2 tables in my database: --'#' denotes the primary key [Libraries] #ID #Application Name 1 MyApp Title 1 2 MyApp Title 2 [Content] #ID Application LibraryID Content 10 MyApp 1 xxx 11 MyApp 1 ...

Update statement using current value of the table

I want to update a row in a table by incrementing by one the integer value of one of the field.. The current doesn't work, why? Update htmIndex SET numObs = numObs+1 where ... ...

Sql 2008 Developer to Sql Azure Migration

Hi My company is deciding for switching its existing application to azure platform (only Sql Part). So we need to upload our db from local to cloud. For migration i came across various tools like 1. cerebrata 's tools 2. SqlAzure Migration wizard 3. Microsoft Sql Data Sync 4. Conventional Script way via management studio. But all the a...

Complex TSQL order by clause

Is it possible to craft an ORDER BY clause to ensure the following criteria for two fields (both of type INT), called child and parent respectively for this example. parent references child, but can be null. A parent can have multiple children; a child only one parent. A child cannot be a parent of itself. There must exist at least one...

improving query times a sql ip lookup database

Hi, I have a table in sql server 2005 which holds an ip range and the corresponding info (country / city / etc). There are approximately 3 million rows and it currently takes just over half a second to return a record based on the query below. DECLARE @ip BIGINT SELECT @ip=3561360969 SELECT TOP 1 id, ipfrom, ipto, countrycode, countr...

SQL query date null check

Hi, I have the following stored procedure. ALTER PROCEDURE [dbo].[spList_Report] @id INT, @startDate DATETIME = NULL, @endDate DATETIME = NULL, @includeStatus1 BIT, @includeStatus2 BIT, @includeStatus3 BIT, @includeStatus4 BIT AS SET NOCOUNT ON SELECT * FROM tblProducts as products WHERE p...

How do we enable SQL cache dependency in ASP.NET 2.0 ?

I think there are some steps to enable SQL Cache Depdndency : Enabling notifications, changes in web.xml and then using Cache Dependency object. Please help how do I pass through them ? ...

Joining a table on itself

Is there a better way to write this SQL query? SELECT *, (SELECT TOP 1 columnB FROM mytable WHERE mytable.columnC = T1.columnC ORDER BY columnD) as firstRecordOfColumnB FROM (SELECT * FROM mytable WHERE columnA = 'apple') as T1 Notice that columnC is not the primary key. ...

SQL Query to fetch all product categories and whether the product is in it

I have three tables. Product, product categories and product category links. The links table is there as a product can be in more than one category. What I am trying to achieve is a list of ALL the categories with an additional field which states whether for that specific product id, if the product is in that category. SELECT *, l.prod...

What is the best way to create mapping from SQL database to GUI?

I think its quite usual task, but still solutions I saw look not so nice. For example in Qt used approach based on MVC pattern -- you must assign all connections manually. Or I remember one PHP engine where pages were creating from DB schema. Which are other approaches? Which one you are prefer? What are the best practices? ...

How to map small binary objects properly in SQLite/NHibernate combo (wrong type affinity)?

Trying to store property of C#/.NET type byte[] in SQLite. Here is my mapping: <class name="MyClass" lazy="false" table="MyTable"> <property name="MyProperty" type ="BinaryBlob" access="property" /> </class> In SQL server it works like a charm even without the explicit type="BinaryBlob" in the mapping. In SQLite I've tried various ...

Keys/indexes for revisions of user submitted text

I have a table: id:int revision:int text:ntext In general I will need to retrieve the latest revision of text for a particular id, or (considerably less frequently) add a new row containing a new revision for a particular id. Bearing this in mind, it would be a good idea to put indexes on the id and revision columns. I don't have a pr...

Representing "X of Y" in SQL

In my database, I have a lot of courses that are compulsory. Some are elective. However, there are courses of a third kind: A list from which you have to choose X courses. The list (and the number X) is different for each study program. How would you represent this relationally? ...

Can you rename a table using the keyword 'AS' and a select statement?

Hi All, This is probably a stupid question to most of you but I was wondering whether you can rename a column using the 'AS' keyword and a select statement? Here is my SQL: Select Main.EmpId , Associate_List.costCenter, Assignments.Area , Main.Assignments_1 AS ( Select Assignment_Name from ...

SQL: extract date from string

There is a text field called myDate. This field can contain either 1) 'Fiscal year ending someDate' or 2) 'dateA to 'dateB'. In situation 1), I want to set the field date1 = to someDate. In situation 2), I want to set the field date1 = dateA and the field date2 = dateB. All the dates (someDate, dateA, dateB) can be written as 1/1/2000...

SQL - How to get the Unique Key's Column Name from Table

I know how to get the columns from a table using the following SQL statement: SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = 'MYTABLENAME') But how do I just return what the UNIQUE Key's Column name? ...

select * vs select column

if I just need 2/3 columns and I query SELECT * instead of providing those columns in select query. is there any performance degradation regarding more/less I/O or memory ?? the network overhead might be present if I do select * without a need. But in a select operation does the database engine always pulls atomic tuple from the disk ?...

SQL query executing slowly (for some parameter values)

I have a SQL Server 2005 database with several tables. One of the tables is used to store timestamps and message counters for several devices, and has the following columns: CREATE TABLE [dbo].[Timestamps] ( [Id] [uniqueidentifier] NOT NULL, [MessageCounter] [bigint] NULL, [TimeReceived] [bigint] NULL, [DeviceTime] [bigint] NULL, [Devic...