sql

How to update a single table using trigger in MS SQL 2008

I have a table PeroidicDeduction and the fields are ID(auto-increment),TotalDeduction(e.g.it can be loan),Paid(on which the deduction for each month),RemainingAmount, What I want is when every time I insert or update the table---RemainingAmount will get the value of TotalDeduction-SUM(Paid)....and writ the following trigger...but dosen'...

Position of object in database

Hi! I have got model Team and I've got (i.e.) team = Team.first :offset => 20. Now I need to get number of position of my team in db table. I can do it in ruby: Team.all.index team #=> 20 But I am sure that I can write it on SQL and it will be less expensive for me with big tables. ...

SQL update fields of one table from fields of another one.

I'm having two tables: A [ID, column1, column2, column3] B [ID, column1, column2, column3, column4] A table will always be subset of B table (meaning all columns of A are also in B). I want to update a record with a specific ID in B with their data from A for all columns of A. This ID exists both in A and B. Is there an UPDATE syntax ...

how to query with child relations to same table and order this correctly

Hi, Take this table: id name sub_id --------------------------- 1 A (null) 2 B (null) 3 A2 1 4 A3 1 The sub_id column is a relation to his own table, to column ID. subid --- 0:1 --- id Now I have the problem to make a correctly SELECT query to show that the child ro...

Is it a good idea to use a computed column as part of a primary key ?

I've got a table defined as : OrderID bigint NOT NULL, IDA varchar(50) NULL, IDB bigint NULL, [ ... 50 other non relevant columns ...] The natural primary key for this table would be (OrderID,IDA,IDB), but this it not possible because IDA and IDB can be null (they can both be null, but they are never both defined at the same time). Ri...

SQL query: SUM values up to the current record

Basically, having this table: 12.10 2.35 21.45 35.26 I want to, in each record, calculate the sum of all previous records, like this: 12.10 | 12.10 2.35 | 14.45 21.45 | 35.90 35.26 | 71.16 ...

SQL Server dilemma, performance

Hello I am creating app where user can save options witch one is better? to save into user table varchar feeld smthing like ('1,23,4354,34,3') query for this is select * from data where CHARINDEX ( 'L', Providers , 0 ) > 0 create other table where user options are and just add rows select * from data where Providers in (select P...

Conversion failed when converting the varchar value to int

Microsoft SQL Server 2008 (SP1), getting an unexpected 'Conversion failed' error. Not quite sure how to describe this problem, so below is a simple example. The CTE extracts the numeric portion of certain IDs using a search condition to ensure a numeric portion actually exists. The CTE is then used to find the lowest unused sequence num...

php, sql selection

I have a stupid question, I have this table : id_product name value 1 price 10-20 1 type computer 2 price 20-30 3 price 100-200 and I want to select from this table GROUP BY id_product and ORDER BY value WHERE name='price' how can i do this? Thanks a ...

Ant database rebuild script, avoiding interactive prompting

Hi Guys. I'm writing an ant script to rebuild our database i.e. dropping everything and rebuilding from scratch. The problem our DBA adds a Y/N prompt before executing the rest of the script, and therefore we can't call this from an automated build process. Does anyone have any suggestions to circumvent the Y/N prompt? Obviously we c...

SQL Join Statement

Hi I have the following SQL SELECT statement SELECT bar_id, bar_name, town_name, advert_text FROM bar, towns, baradverts WHERE town_id = town_id_fk AND bar_id = bar_id_fk My problem is that since not every bar has an advert in table "baradverts", these bars are not coming up in the results. In other words I need a NULL for those...

PHP problem with selecting from Oracle global temporary table

Hello, I have an Oracle global temporary table which is "ON COMMIT DELETE ROWS". I have a loop in which I: Insert to global temporary table Select from global temporary table (post-processing) Commit, so that the table is purged before next iteration of the loop Insertion is done with a call to oci_execute($stmt, OCI_DEFAULT). Retr...

Using 'in' in Join

i have two selects a & b and i join them like: select * from ( select n.id_b || ',' || s.id_b ids, n.name, s.surname from names n, surnames s where n.id_a = s.id_a ) a left join ( select sn.id, sn.second_name ) b on b.id in (a.ids) in this case join doesn't work :( The problem is in b.id in (a.ids). But why if it...

Find telephonenumbers - finding number with and without an phone extension

Hello there I've a table with about 130 000 records with telephonenumbers. The numbers are all formated like this +4311234567. The numbers always include international country code, local area code and then the phonenumber and sometimes an extension. There is a webservice which checks for the caller's number in the table. That servic...

How to select data with a certain minimum total?

I have one table bbc(country, region, area, population, gdp). I want to select the regions with a total population of at least 100 million. How can I do it with SQL? ...

SQL 2008 Restore from device issue

Hey chaps, I have following situation: SQL Server 2008 database, in full recovery model I created a full backup, and an hour later a transaction log backup (both to separate files, so I have a .bak file, and a .trn file) Now for testing purposes, I want to restore the database using these two files. I am doing this on the same serve...

Foreach time loop not working

There are a few parts to this code. The first part is creating fixtures. $totalRounds = $teams - 1; $matchesPerRound = $teams / 2; $rounds = array(); $roundDates = array(); $curTime = time(); for ($i = 0; $i < $totalRounds; $i++) { $rounds[$i] = array(); $numDays = $i * 4; $roundDates[$i] = s...

How to use multiple identity numbers in one table?

I have an web application that creates printable forms, these forms have a unique number on them, the problem is I have 2 forms that separate numbers need to be created for them. ie) Form1- Numbered 2000000-2999999 Form2- Numbered 3000000-3999999 dbo.test2 - is my form information table Tsel - is my autoinc table for the 3000000 ser...

mysql select records greater than 3 months

I am trying to write a query to select all records from users table where User_DateCreated (datetime field) is >= 3 months from today. Any ideas? Thanks! ...

UTF-8 vs ASCII Text

Why does sql database use UTF-8 Encoding? do they both use 8-bit to store a character? ...