Inserting last two digits from column into new column - sql

So I'm supposed to take the last two digits from the phone number, and insert it into a new column in the same table.
I'm currently getting this error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'Pass_id', table 'lunches.dbo.passenger'; column does not allow nulls. INSERT fails.
The statement has been terminated.
This is the query I'm using to get this error:
INSERT INTO dbo.passenger (age)
SELECT
RIGHT(phone, 2)
FROM dbo.passenger

UPDATE dbo.passenger
SET age = Cast(Right(phone, 2) As tinyint)
WHERE phone IS NOT NULL
AND Right(phone, 2) NOT LIKE '%[^0-9]%'

Related

Adding columns and data to table

I am trying to run the following queries. When I run them separately the code executes correctly. When I run them together I get the error message:
Msg 207, Level 16, State 1, Line 11
Invalid column name 'TotOP'.
Msg 207, Level 16, State 1, Line 12
Invalid column name 'TotPK'.
It's as if the first query has not run at all? - Any ideas
As a work around I know that I can generate all of the columns at the beginning and only partially populate the table leaving these final two columns empty until I run the second query however I am curious to know why these statements can be run separately but not together.
Query 1:
ALTER TABLE [fcgen].[RPD1yeardata]
ADD [TotOP] DECIMAL (7,3) NULL,
[TotPK] DECIMAL (7,3) NULL;
Query 2:
UPDATE [fcgen].[RPD1yeardata]
SET [TotOP] = [1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+[11]+[12]+[13]+[14]+[39]+[40]+[41]+[42]+[43]+[44]+[45]+[46]+[47]+[48],
[TotPK] = [15]+[16]+[17]+[18]+[19]+[20]+[21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]+[32]+[33]+[34]+[35]+[36]+[37]+[38]
FROM [fcgen].[RPD1yeardata]
just use GO after 1s query
ALTER TABLE [fcgen].[RPD1yeardata]
ADD [TotOP] DECIMAL (7,3) NULL,
[TotPK] DECIMAL (7,3) NULL;
Go
--Query 2:
UPDATE [fcgen].[RPD1yeardata]
SET [TotOP] = [1]+[2]+[3]+[4]+[5]+[6]+[7]+[8]+[9]+[10]+[11]+[12]+[13]+[14]+[39]+[40]+[41]+[42]+[43]+[44]+[45]+[46]+[47]+[48],
[TotPK] = [15]+[16]+[17]+[18]+[19]+[20]+[21]+[22]+[23]+[24]+[25]+[26]+[27]+[28]+[29]+[30]+[31]+[32]+[33]+[34]+[35]+[36]+[37]+[38]
FROM [fcgen].[RPD1yeardata];

How to Use the OUTPUT of an Insert Query for another insert query

I'm trying to insert 2 records into 2 tables by using a subquery, but it gives me a syntax error.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'INSERT'.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.
The query that I'm trying to execute is
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
VALUES
(
(INSERT INTO [FileTransformations] OUTPUT INSERTED.FileTransformationId DEFAULT VALUES),
2
)
Is this possible?
You need to store the output in a variable first. So, I think you want something like this:
DECLARE #variable TABLE (value INT)--change type depending on your need
INSERT INTO [FileTransformations]
OUTPUT INSERTED.FileTransformationId INTO #variable(value)
VALUES(.....)
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
SELECT value, 2 FROM #variable
See How do I use an INSERT statement's OUTPUT clause to get the identity value? for more info on using output variables and Insert into table from table variable? for then inserting from that table variable.
Another option, if you just want the id of the last inserted record is to use scope_identity(). So you could write the above as:
INSERT INTO [FileTransformations]
VALUES(.....)
INSERT INTO [Files] ([FileTransformationId],[FileTypeEnumId])
VALUES scope_identity(), 2

SQL Insert invalid column name non-numeric values only

I am writing a very basic insert statement. I noticed that the error message "Invalid column name" appears if the VALUE is a character besides a number.
The data type is VARCHAR(50) on all three columns.
Database: MS SQL 2012
Query:
INSERT INTO [test-db].[dbo].[Computer]
([id]
,[name]
,[office])
VALUES
(123
,john
,main)
GO
ERROR:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'john'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'main'.
Change your query to this
INSERT INTO [test-db].[dbo].[Computer]
([id]
,[name]
,[office])
VALUES
(123
,'john'
,'main')
GO
The varchar needs quotes around it to work.

SQL rollup two columns error

i am having error in SQL saying:
Msg 213, Level 16, State 1, Line 7
Column name or number of supplied values does not match table definition.
Code:
CREATE TABLE temp
(
kolA varchar(255),
kolB varchar(255)
);
INSERT temp VALUES
('A','B'),
('B','B'),
('B','B'),
('A','B'),
(null,'B'),
('B','B');
select kolA,kolB,ilośc = COUNT(*) from temp
GROUP BY rollup(kolA,kolB);
DROP TABLE temp
i do not know why this error occurs, can someone tell me?
This works fine against 2008 in a fiddle.
http://sqlfiddle.com/#!3/61dc9d/1

Insert issue Msg 213, Level 16, State 1, Line 2

General newbie SQL question probs...
Why doesn't this work - or, what does this mean?
INSERT INTO test_db.dbo.Customers2
SELECT FirstName
FROM jas_test_db.dbo.Customers
WHERE (Customers.FirstName = Firstname)
I get:
Msg 213, Level 16, State 1, Line 2
Column name or number of supplied values does not match table definition.
You should supply column name for Customers2:
INSERT INTO test_db.dbo.Customers2(**Firstname**)
SELECT FirstName
FROM jas_test_db.dbo.Customers
WHERE (Customers.FirstName = Firstname)
I would recommend to always explicitly specify which columns you want to insert into:
INSERT INTO test_db.dbo.Customers2(FirstName) -- <-- specify the columns you want to insert into!
SELECT FirstName
FROM jas_test_db.dbo.Customers
WHERE (Customers.FirstName = Firstname)
If you don't do this, you have to provide values for all columns, in the exact order in which they are defined in the table