SQL server insert same value to multiple rows - sql

How can I insert the same value to multiple rows?
For example, I want to insert $10 dollars to all customers bill table Bill_Amount column where customer is equal to specific service type
I want to do it in SQL Server.

Assuming you have a customers table, you could use the insert-select syntax and select a literal of 10$:
INSERT INTO bill_amount (customer_id, bill_amount)
SELECT customer_id, 10
FROM customers
WHERE service_type = 'some_service'

Related

How can I make this an insert statement

I am new to SQL, So I have a table called 'DealerShip' and I have a many different car ID's . I have a dealership called 'Hondo' and another one called 'Mitch' . I would like to insert about 80 records into 'Mitch' that 'Hondo' has . For instance with this Query
select * from DealerShips where name='Hondo' and CarType=63
That query above contains about 70 records, How can I create an insert statement that will insert all the returned records from that query above ? The insert will go into the same table above except that the name will be 'Mitch' . I am using MSSQL 2012
INSERT INTO SELECT
INSERT INTO yourtable (FIELDS...) ---- fields here should match the select fields
SELECT FIELDS... FROM
DealerShips where name = 'Hondo' and CarType=63
Just make sure you list the columns in the insert and the select in the same order.
INSERT INTO DealerShips (name, cartype, more columns)
SELECT
'Mitch'
, cartype
, more columns
from DealerShips where name='Hondo' and CarType=63

SQL Query and Sort From Multiple Tables

Working with SQL via a NOVA Oracle DB. Need to know how to query from multiple tables and arrange results based on being sorted by the highest values. Here are a few lines of code to reflect the three tables:
INSERT INTO VEHICLES
(vehicleVIN,vehicleType,vehicleMake,vehicleModel,vehicleWhereFrom,vehicleWholesaleCost,vehicleTradeID)
VALUES
('147258HHE91K3RT','compact','chevrolet','spark','Maryland',20583.00,NULL);
INSERT INTO VEHICLES
(vehicleVIN,vehicleType,vehicleMake,vehicleModel,vehicleWhereFrom,vehicleWholesaleCost,vehicleTradeID)
VALUES
('789456ERT0923RFB6','Midsize','ford','Taurus','washington, d.c.',25897.22,1);
INSERT INTO VEHICLES
(vehicleVIN,vehicleType,vehicleMake,vehicleModel,vehicleWhereFrom,vehicleWholesaleCost,vehicleTradeID)
VALUES
('1234567890QWERTYUIOP','fullsize','Lincoln','towncar','Virginia',44222.10,NULL);
AND
INSERT INTO SALES
(saleID,grossSalePrice,vehicleStatus,saleDate,saleMileage,customerID,salespersonID,vehicleVIN)
VALUES
(1,25987.28,'sold',date '2012-10-15',10,1,1,'147258HHE91K3RT');
INSERT INTO SALES
(saleID,grossSalePrice,vehicleStatus,saleDate,saleMileage,customerID,salespersonID,vehicleVIN)
VALUES
(2,29999.99,'sold',date '2012-10-17',50087,2,2,'789456ERT0923RFB6');
INSERT INTO SALES
(saleID,grossSalePrice,vehicleStatus,saleDate,saleMileage,customerID,salespersonID,vehicleVIN)
VALUES
(3,47490.88,'sold',date '2012-11-05',30,3,3,'1234567890QWERTYUIOP');
AND
INSERT INTO CUSTOMERS
(customerID,customerFirName,customerLasName,customerMiName,customerStreet,customerState,customerCity,customerZip)
VALUES
(1,'Regorna','Trasper','J','11111 Address Way','Maryland','Hollywood','20636');
INSERT INTO CUSTOMERS
(customerID,customerFirName,customerLasName,customerMiName,customerStreet,customerState,customerCity,customerZip)
VALUES
(2,'Bob','Seagram','A','22222 Seagram Lane','Texas','Houston','77001');
INSERT INTO CUSTOMERS
(customerID,customerFirName,customerLasName,customerMiName,customerStreet,customerState,customerCity,customerZip)
VALUES
(3,'Sally','Anderson','P','33333 Pheonix Drive','Arizona','Pheonix','85001');
Obviously there are other tables that come into play here (salesperson, etc.), however these are the only tables needed for the query. The query I want to pull needs to show the total count of sales for each model, sorted by the highest values, and the total count of sales for each zip code, sorted by the highest values. An example (using the data provided above) would look similar to this:
MODEL NUMBER of SALES ZIP CODE NUMBER OF SALES
spark 1 20636 1
Taurus 1 77001 1
towncar 1 85001 1
The results need to be sorted by highest values, based on the number of sales. I'm also trying to accomplish this via a single SELECT query.
I've tried some ideas, but haven't been able to find anything that hits the home run yet. Thanks for the help!
See if this is what you're after:
SELECT DISTINCT v.VEHICLEMODEL, COUNT(*) OVER (PARTITION BY s.VEHICLEVIN) "CAR_SALES"
, c.CUSTOMERZIP, COUNT(*) OVER (PARTITION BY c.CUSTOMERZIP )"TOTAL_SALES_AT_ZIP"
FROM SALES s, VEHICLES v, CUSTOMERS c
WHERE s. VEHICLEVIN = v. VEHICLEVIN
and c. CUSTOMERID = s. CUSTOMERID
ORDER BY 2 DESC , 4 DESC

SQL On Delete Trigger

I am trying to create a trigger that when a row is deleted from a table it gets inserted in another table:
1 Create Or Replace Trigger cancel
2 After Delete
3 On OrderTable
4 For EACH ROW
5 Begin
6 Insert Into CancelledOrders Values (:old.acctNum, age, phone)
7 From OrderTable Natural Join Customer
8 Where acctid = :old.acctNum AND menuid = :old.menuNum;
9 End;
10 /
11 Show Errors;
I want to grab the acctNum, age, and phone. The acctNum is from the Order table but the age and phone is from the Customer table. Therefore I join the two tables (on the acctid key). So the joined result will look like this:
acctNum Age Phone
I get this error when I try to compile the Trigger:
2/2 PL/SQL: SQL Statement ignored
3/2 PL/SQL: ORA-00933: SQL command not properly ended
Does anyone know the problem?
EDIT:
Table Structure:
OrderTable: AcctNum MenuNum startOrder endOrder
Customer Table: AcctNum age phone
You're mixing the values and select (subquery) syntax, which are for different things. You can insert from a query that uses a value from the :old pseudorecord and values from the customer table:
Insert Into CancelledOrders -- (acctNum, age, phone)
Select :old.acctNum, age, phone
From Customer
Where acctNum = :old.acctNum;
It's better to specify the columns in the target table as part of the insert clause (I've left that commented out in case the names are different). You also don't want (or need) to requery the table the trigger is against; you already have the data you need, and it will get a mutating-table error in some circumstances. So no join is needed.
Your insert statement is incorrect. You need to specify the columns you want to select in the tables.
Add the select clause to your insert statement. Also, removed the values keyword and specify column names:
Insert Into CancelledOrders (acctNum, age, phone)
Select :old.acctNum, age, phone
From OrderTable Natural Join Customer
Where acctid = :old.acctNum AND menuid = :old.menuNum;

Get list of record from one to many table sql server

I have two table which have one to many relationship
Example :
Table Buy : BuyID, BuyName
Table BuyItems: BuyItemID, Buy_BuyID, Quantity, Amount
Now I want to create trigger so whenever user insert to table buy, I can insert to table :
Buy_Summary :
TotalQuantity (which is sum from all quantity in table BuyItems which from record Buy.BuyID=BuyItems.Buy_BuyID)
TotalAmount (which is sum from all amount in table BuyItems which from record Buy.BuyID=BuyItems.Buy_BuyID)
How can I do this?

Insert into table select from table a and table b where

I want to insert data into a table by joining two tables with a where condition that each id matches.
INSERT INTO SALES(T_ID, SF)
SELECT B.T_ID, B.SF
FROM HIS B, SALES C
WHERE C.REP_ID=B.REP_ID;
I am getting an error that I cannot insert NULL into ("c.REP_ID")
I am not trying to insert anything into c.rep_id. I want to insert values into t_id, sf from HIS table where the rep_id on his table = rep id on sales table.
INSERT INTO SALES (T_ID, SF)
SELECT h.T_ID, h.SF FROM HIS h, SALES C
WHERE C.REP_ID=B.REP_ID;
Note: Make sure that the data comes from select tables is equally compare and valid with the datatypes of the columns where you inserting.
The error simply means that the column REP_ID in the SALES table has a NOT NULL constraint. Your INSERT statement doesn't insert any value into that column (you only insert T_ID and SF) and presumably there are no before-row triggers that will set that column for you.