Getting result from select command [SQL] - sql

This is my table:
INSERT INTO Emp (EmpNo, EName, Job, Mgr, HireDate, Sal, Comm, DeptNo)
VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30),
(7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30),
(7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, NULL, 20),
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30),
(7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, NULL, 30),
(7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, NULL, 10),
(7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19', 3000, NULL, 20),
(7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10),
(7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30),
(7876, 'ADAMS', 'CLERK', 7788, '1987-05-23', 1100, NULL, 20),
(7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950,NULL, 30),
(7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, NULL, 20),
(7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, NULL, 10);
Now I want to get every worker who their job isn't PRESIDENT or MANAGER.
This is my command:
SELECT *
FROM [dbo].EMP
WHERE NOT Job = 'MANAGER' OR Job = 'PRESIDENT';
The result is almost correct, but I get one PRESIDENT who shouldn't be there.
This one:
7839 KING PRESIDENT NULL 1981-11-17 5000.00 NULL 10

You need to wrap it with brackets:
SELECT * FROM [dbo].EMP WHERE NOT (Job='MANAGER' OR Job='PRESIDENT');
Or use NOT IN:
SELECT * FROM [dbo].EMP WHERE Job NOT IN ('MANAGER', 'PRESIDENT');

Use NOT IN
SELECT * FROM [dbo].EMP WHERE Job NOT in ('MANAGER' ,'PRESIDENT')

Related

PSQL / PgAdmin query issue

Learning Sql, this query returns a syntax error in Pgadmin and Sql shell says there are more entries than target yet there are 8 entries for 8 columns in my table. I've tried specifying the target columns as i've read its good practice to no avail. I'm going off of a course for this, its another random exercise, but the course is frankly useless mixes commands for psql oracle or straight up incorrect ones, basically nothing in it works as it describes. While i'm used to it can't say it helps and leaves me stuck on what's overwhelmingly likely basic syntax written wrong.
Ty for your time.
INSERT INTO EMP(noemp, nomemp, emploi, mgr, dateemb, sal, comm, nodept)
VALUES (
(7369, 'SERGE', 'FONCTIONNAIRE', 7902, TO_DATE('17-DEC-1980', 'DD-MON-YYYY'), 800, NULL, 20),
(7499, 'BRAHIM', 'VENDEUR', 7698, TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30),
(7521, 'NASSIMA', 'VENDEUR', 7698, TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30),
(7566, 'LUCIE', 'GESTIONNAIRE', 7839, TO_DATE('12-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20),
(7654, 'MARTIN', 'VENDEUR', 7698, TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30),
(7698, 'BENJAMIN', 'GESTIONNAIRE', 7839, TO_DATE('01-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30),
(7782, 'DAYANE', 'GESTIONNAIRE', 7839, TO_DATE('09-JUNE-1981', 'DD-MON-YYYY'), 2450, NULL, 10),
(7788, 'ARIJ', 'ANALYSTE', 7566, TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3050, NULL, 20),
(7839, 'MAYAR', 'PRESIDENT', NULL, TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10),
(7844, 'ROI', 'VENDEUR', 7698, TO_DATE('08-SEP-1981', 'DD-MON-YYYY'), 1500, 0, 30),
(7876, 'VIRGINIE', 'FONCTIONNAIRE', 7788, TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20),
(7900, 'LYNA', 'FONCTIONNAIRE', 7698, TO_DATE('03-DEC-1981', 'DD-MON-YYYY'), 950, NULL, 30),
(7902, 'ASMA', 'ANALYSTE', 7566, TO_DATE('03-DEC-1981', 'DD-MON-YYYY'), 3000, NULL, 20),
(7934, 'SIMONE', 'FONCTIONNAIRE', 7782, TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10)
);
Edit: Adding parenthseses (which i add at some point, cf below gets) rid of line 4 error and returns line 11 error instead, same text.
Edit2: TLdr there was a typo on one month and lacked proper formatting. Ty all.
I found a french month name in your SQL Code (JUIN) :
7782, ’DAYANE’, ’GESTIONNAIRE’, 7839, TO_DATE(’9-**JUIN**-1981', ’DD-MON-YYYY’), 2450, NULL, 10,
it must be remplaced by 'JUNE'.
Try to put each record between parenthesis like that :
INSERT INTO EMP(noemp, nomemp, emploi, mgr, dateemb, sal, comm, nodept)
VALUES (7369, 'SERGE', 'FONCTIONNAIRE', 7902, TO_DATE('17-DEC-1980', 'DD-MON-YYYY'), 800, NULL, 20),
(7499, 'BRAHIM', 'VENDEUR', 7698, TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30),
(7521, 'NASSIMA', 'VENDEUR', 7698, TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30),
(7566, 'LUCIE', 'GESTIONNAIRE', 7839, TO_DATE('12-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20),
(7654, 'MARTIN', 'VENDEUR', 7698, TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30),
(7698, 'BENJAMIN', 'GESTIONNAIRE', 7839, TO_DATE('01-MAY-1981', 'DD-MON-YYYY'), 2850, NULL, 30),
(7782, 'DAYANE', 'GESTIONNAIRE', 7839, TO_DATE('9-JUNE-1981', 'DD-MON-YYYY'), 2450, NULL, 10),
(7788, 'ARIJ', 'ANALYSTE', 7566, TO_DATE('09-DEC-1982', 'DD-MON-YYYY'), 3050, NULL, 20),
(7839, 'MAYAR', 'PRESIDENT', NULL, TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10),
(7844, 'ROI', 'VENDEUR', 7698, TO_DATE('08-SEP-1981', 'DD-MON-YYYY'), 1500, 0, 30),
(7876, 'VIRGINIE', 'FONCTIONNAIRE', 7788, TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20),
(7900, 'LYNA', 'FONCTIONNAIRE', 7698, TO_DATE('03-DEC-1981', 'DD-MON-YYYY'), 950, NULL, 30),
(7902, 'ASMA', 'ANALYSTE', 7566, TO_DATE('03-DEC-1981', 'DD-MON-YYYY'), 3000, NULL, 20),
(7934, 'SIMONE', 'FONCTIONNAIRE', 7782, TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10)
You can verify it with this link

Why is my sql script returning "[null]" and "0" values even though my table is populated?

I have manually populated a table as follows:
INSERT INTO country VALUES
-- columns are countryid, name, continent, population, gdp, lifeexpectancy, founded
(1, 'Argentina', 36.3, 348.2, 70.75, 9/7/1816),
(2, 'Brazil', 183.3, 1004, 65.6, 9/7/1822),
(3, 'Canada', 30.1, 658, 77.08, 1/7/1867),
(4, 'England', 60.8, 1256, 75.15, NULL),
(5, 'France', 60, 1000, 75.15, 14/7/1789),
(6, 'Mexico', 107.5, 694.3, 69.36, 16/9/1821),
(7, 'USA', 270, 8003, 75.75, 4/7/1776),
(8, 'Cuba', 11.7, 16.9, 75.95, 24/2/1895),
(9, 'Guatemala', 20, 200, 60, 15/9/1821),
(10, 'Tanzania', 55.57, 47.43, 60.76, 9/12/1961),
(11, 'India', 1324, 2264, 68.35, NULL),
(12, 'South Africa', 55.91, 294.8, 57.44, 31/05/1910),
(13, 'Costa Rica', 4.86, 57.44, 79.59, 15/9/1821),
(14, 'Uganda', 41.49, 25.53, 59.18, NULL);
but when I query
SELECT lifeexpectancy FROM country;
The tables returns a combination of "0" and "[null]" values.
I've tried changing the data_type of lifeexpectancy. Neither text nor numeric data types return values that have been entered.
Any suggestions?
You are not inserting the continent. Presumably you want that, but it is not in your sample data.
You should be using standard date formats. In most databases, YYYY-MM-DD works. And you should list the columns. So:
INSERT INTO country (countryid, name, population, gdp, lifeexpectancy, founded)
VALUES (1, 'Argentina', 36.3, 348.2, 70.75, '1816-07-09'),
(2, 'Brazil', 183.3, 1004, 65.6, '1822-07-09'),
(3, 'Canada', 30.1, 658, 77.08, '1867-07-01'),
(4, 'England', 60.8, 1256, 75.15, NULL),
(5, 'France', 60, 1000, 75.15, '1789-07-14'),
(6, 'Mexico', 107.5, 694.3, 69.36, '1821-09-16'),
(7, 'USA', 270, 8003, 75.75, '1776-07-04'),
(8, 'Cuba', 11.7, 16.9, 75.95, '1895-02-24'),
(9, 'Guatemala', 20, 200, 60, '1821-09-15'),
(10, 'Tanzania', 55.57, 47.43, 60.76, '1961-12-09'),
(11, 'India', 1324, 2264, 68.35, NULL),
(12, 'South Africa', 55.91, 294.8, 57.44, '1910-05-31'),
(13, 'Costa Rica', 4.86, 57.44, 79.59, '1821-09-15'),
(14, 'Uganda', 41.49, 25.53, 59.18, NULL);
I believe you're supplying 6 values instead of 7 (may be because of Auto Increment), if that the case you need to specify the columns here.
Also pass date with single quotes surrounded:
INSERT INTO country (countryid, name, continent, population, gdp, lifeexpectancy, founded)
VALUES
(1, 'Argentina', 36.3, 348.2, 70.75, '9/7/1816'),
(2, 'Brazil', 183.3, 1004, 65.6, '9/7/1822'),
(3, 'Canada', 30.1, 658, 77.08, '1/7/1867'),
(4, 'England', 60.8, 1256, 75.15, NULL),
(5, 'France', 60, 1000, 75.15, '14/7/1789'),
(6, 'Mexico', 107.5, 694.3, 69.36, '16/9/1821'),
(7, 'USA', 270, 8003, 75.75, '4/7/1776'),
(8, 'Cuba', 11.7, 16.9, 75.95, '24/2/1895'),
(9, 'Guatemala', 20, 200, 60, '15/9/1821'),
(10, 'Tanzania', 55.57, 47.43, 60.76, '9/12/1961'),
(11, 'India', 1324, 2264, 68.35, NULL),
(12, 'South Africa', 55.91, 294.8, 57.44, '31/05/1910'),
(13, 'Costa Rica', 4.86, 57.44, 79.59, '15/9/1821'),
(14, 'Uganda', 41.49, 25.53, 59.18, NULL);

Get the right data for a specific id

I want to get the prices for each product for a specific place. Here's what I have done so far.
SELECT pl.id AS place_id,
pl.data_name AS place_name,
pp.data_price AS product_price,
pp.date_updated AS price_updated
FROM places AS pl
JOIN products AS pr
ON pl.id = pr.id_place
JOIN products_prices AS pp
WHERE pp.id_product = '30'
GROUP BY pl.id, pp.data_price, pp.date_updated
ORDER BY pp.data_price DESC, pp.date_updated DESC
As you can see from the image above, product_price and price_updated are all the same. place_name also shows 3 places that does not have the product with id 30.
Here's how it should looks like (cheapest, most recent product purchase on top):
place_id place_name product_price price_updated
3 ICA Maxi 4.95 2018-05-16
1 ICA Supermarket 5.90 2018-05-27
26 ICA Skutan 6.50 2018-05-29
Here's the database structure:
CREATE TABLE IF NOT EXISTS `places` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data_name` tinytext NOT NULL,
`data_address` text,
`data_address_city` tinytext NOT NULL,
`data_coordinates` varchar(50) NOT NULL,
UNIQUE KEY `id` (`id`)
);
INSERT INTO `places` (`id`, `data_name`, `data_address`, `data_address_city`, `data_coordinates`) VALUES
(1, 'ICA Supermarket', 'Björkhagsgatan 9', 'Skoghall', '59.324971,13.467291'),
(2, 'ICA Maxi', 'Bergviks Köpcentrum', 'Karlstad', '59.376563,13.428787'),
(3, 'ICA Kvantum', 'Bivägen 11', 'Hammarö', '59.343388,13.504583'),
(4, 'IKEA', 'Bergviksvägen 43', 'Karlstad', '59.379032,13.420646'),
(5, 'Karlstad Naprapatklinik', 'Västra Torggatan 15', 'Karlstad', '59.381379,13.501683'),
(9, 'Besök i Borgvik AB', '', 'Borgvik', '59.348261,12.954707'),
(23, 'Mariebergsskogen', '', 'Karlstad', '59.369403,13.486485'),
(24, 'Happy Price', 'Brehogsvägen 20', 'Tanumshede', '58.723730,11.344768'),
(25, 'Trekanten Kök & Bar', 'Parkvägen 2', 'Hamburgsund', '58.552733,11.270998'),
(26, 'ICA Skutan', 'Strandvägen', 'Hamburgsund', '58.552122,11.270898');
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_place` int(11) NOT NULL,
`data_barcode` text NOT NULL,
`data_name` text NOT NULL,
`data_weight` text NOT NULL,
`data_weight_type` text NOT NULL,
UNIQUE KEY `id` (`id`)
);
INSERT INTO `products` (`id`, `id_place`, `data_barcode`, `data_name`, `data_weight`, `data_weight_type`) VALUES
(7, 3, '7311070008494', 'Pågen Lantbröd', '650', 'g'),
(8, 3, '7310618084808', 'Grumme citronsåpa', '750', 'ml'),
(9, 3, '7318690079835', 'ICA Basic toalettpapper', '', ''),
(12, 1, '7318690134640', 'ICA Basic Milk & Caramel', '100', 'g'),
(18, 3, '7310380512103', 'ICA Home dishmatic refillsvamp', '', ''),
(19, 3, '7340109200684', 'Plastkasse', '', ''),
(20, 1, '7310751163903', 'ICA tonfisk filébitar i vatten', '185', 'g'),
(24, 1, '7310865001818', 'Arla mellanmjölk', '1.5', 'kg'),
(25, 1, '7318690079712', 'ICA kattsand (klumpbildande, ej parfym)', '6', 'kg'),
(29, 26, '3068320055008', 'Evian mineralvatten', '500', 'mL'),
(30, 26, '7318690134640', 'ICA Basic Milk & Caramel', '100', 'g'),
(33, 2, '0', 'ICA Basic Milk & Caramel', '100', 'g');
CREATE TABLE IF NOT EXISTS `products_prices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_product` int(11) NOT NULL,
`id_transaction` int(11) NOT NULL,
`data_price` decimal(10,2) NOT NULL,
`data_amount` tinytext NOT NULL,
`data_discount` tinytext,
`data_discount_amount` tinytext,
`data_discount_sum` tinytext,
`data_pant` tinytext,
`date_updated` date NOT NULL,
UNIQUE KEY `id` (`id`)
);
INSERT INTO `products_prices` (`id`, `id_product`, `id_transaction`, `data_price`, `data_amount`, `data_discount`, `data_discount_amount`, `data_discount_sum`, `data_pant`, `date_updated`) VALUES
(1, 1, 907, 18.90, '4', '', '2', '30', NULL, '2018-05-18'),
(2, 12, 907, 4.90, '4', '', '', '', NULL, '2018-05-18'),
(5, 4, 904, 18.90, '1', '', '', '', NULL, '2018-05-18'),
(6, 14, 904, 14.90, '1', '', '', '', NULL, '2018-05-18'),
(17, 19, 936, 2.00, '1', '', '', '', NULL, '2018-05-21'),
(18, 21, 947, 23.90, '1', '', '', '', NULL, '2018-05-23'),
(19, 22, 947, 10.50, '1', '', '', '', NULL, '2018-05-23'),
(20, 20, 947, 15.90, '1', '', '', '', NULL, '2018-05-23'),
(21, 13, 947, 49.90, '1', '', '', '', NULL, '2018-05-23'),
(22, 23, 948, 14.90, '1', '', '', '', NULL, '2018-05-24'),
(23, 24, 961, 14.90, '1', NULL, NULL, NULL, NULL, '2018-05-27'),
(24, 12, 961, 5.90, '4', NULL, NULL, NULL, NULL, '2018-05-27'),
(32, 28, 967, 8.50, '2', NULL, NULL, NULL, NULL, '2018-05-28'),
(33, 29, 972, 12.90, '1', NULL, NULL, NULL, '1', '2018-05-29'),
(34, 30, 973, 6.50, '2', NULL, NULL, NULL, NULL, '2018-05-29'),
(35, 31, 976, 10.00, '1', NULL, NULL, NULL, NULL, '2018-05-30'),
(36, 32, 976, 10.00, '1', NULL, NULL, NULL, NULL, '2018-05-30');
As mentioned in my comment to your request: You forgot the ON clause that links the products_prices table to the other two.
MariaDB nastily converts your inner join into a cross join instead of raising an error as it should. What this gives you is: select all prices for product 30 combined with all places multiplied with the number of products therein, instead of just combined with the product's place.
By the way: I'd put table products first in the FROM clause, because this is the base (you want product 30). Then join the product's prices and the product's place.
There is no reason to group your data, as you only want to list the prices. You are not aggregating anything. So you get:
SELECT
pl.id AS place_id,
pl.data_name AS place_name,
pp.data_price AS product_price,
pp.date_updated AS price_updated
FROM products AS pr
JOIN products_prices AS pp ON pp.id_product = pr.id
JOIN places pl ON pl.id = pr.id_place
WHERE pr.id = 30
ORDER BY pp.data_price DESC, pp.date_updated DESC;
Try this:
SELECT PL.id AS place_id,
PL.data_name AS place_name,
PP.data_price AS product_price,
PP.date_updated AS price_updated
FROM products_prices PP
JOIN products PR
ON PR.id = PP.id_product
JOIN places PL
ON PL.id = PR.id_place
WHERE PR.id = '30'

nested select TOP 1 statement

I am working on a system currently for Asset Management within our company.
The system allows users to sign in/out items on either a permenant or temporary basis.
I am working on some reporting on the system and have hit a stumbling block with returning the relevent data.
So i know that i need a nested Select TOP 1 statement within my main query but i cant get it to work.
The main query is as follows:
SELECT [Asset-User].ID, [Asset-User].Asset_ID, [Assets].ID, [Assets].Signed_Out, [Assets].Asset_Tag_Serial_Num, [Assets].Name_Hostname, [Assets].Type_ID, [Asset_Type].ID, [Asset_Type].Name_Model, [Asset-User].User_ID, [Company_Users].ID, [Asset-User].Sign_Out_Date, [Asset-User].Return_Date, [Asset-User].[Perm_Signout?]
FROM [Asset-User], [Assets], [Asset_Type], [Company_Users]
WHERE ([User_ID] = '1') AND [Asset-User].Asset_ID = [Assets].ID AND [Assets].Type_ID = [Asset_Type].ID AND [Asset-User].User_ID = [Company_Users].ID AND [Assets].Signed_Out = '1'
So this query returns everything that is currently marked as "Signed Out" that has ever been signed out by User 1.
Somewhere in here i need to add a nested Select TOP 1 on Asset-User.ID so that it only returns items that are marked as permanent sign outs, OR if not permanent that they have most recently been signed out by User 1.
This would then only give me a list of items currently assigned to that user and not display the Asset if someone else has signed it out since
Any help would be greatly appreciated
As per request Sample data below:
https://docs.google.com/spreadsheets/d/1o4T6bsxyO-1dGE0-FUtWFboRupcq4o4V9i2Em0_BjyU/edit?usp=sharing
First sheet shows actual results, second sheet shows roughly what should be expected
As you will see here, this user has signed out a few items multiple times (and those items may not most recently have been signed out by this user)
Its hard to get the sample data but in essence there should be no duplicate Asset_ID's in the list as only the most recent ID (sign out ID this relates to) should be displayed for each Asset_ID.
Does that help?
Thanks again
As per request here is the Schema build code that will create a sample DB to work with
CREATE TABLE Asset_Type
([ID] int, [Global_ID] int, [Name_Model] varchar(30), [Description_Spec] varchar(54))
;
INSERT INTO Asset_Type
([ID], [Global_ID], [Name_Model], [Description_Spec])
VALUES
(1, 1, 'Dell Optiplex 3020', 'Windows 7 Professional, Intel Core i3 3.40GHz,4Gb RAM'),
(2, 3, 'Viewsonic VA2231wa', 'Viewsonic Widescreen Monitor'),
(3, 3, 'Samsung S24B150BL', 'Samsung LED 24" Widescreen Monitor')
;
CREATE TABLE Assets
([ID] int, [Asset_Tag_Serial_Num] varchar(29), [Type_ID] int, [Purchase_Date] varchar(10), [Purchase_Price] varchar(7), [Name_Hostname] varchar(36), [Signed_Out] int)
;
INSERT INTO Assets
([ID], [Asset_Tag_Serial_Num], [Type_ID], [Purchase_Date], [Purchase_Price], [Name_Hostname], [Signed_Out])
VALUES
(1, '0206', 1, '2013-11-29', '323.30', 'WS0206', 1),
(3, '0226', 2, NULL, NULL, 'Viewsonic VA2231wa - 0226', 1),
(4, '0204', 1, '2013-11-29', '323.00', 'WS0204', 1),
(5, '0205', 1, '2013-11-29', '323.00', 'WS0205', 1),
(6, '0108', 1003, NULL, NULL, 'Small Office Sat Nav', 1),
(7, '0092', 1004, NULL, NULL, 'Large Office Sat Nav', 1),
(8, 'GWC36-DHDBC-J2MXY-H2BGY-8C79G', 1005, '1900-01-01', '0.00', 'MS Office for WS0020', 1),
(9, '0020', 1006, '1900-01-01', '0.00', 'WS0020', 1),
(10, '0173', 2, '1900-01-01', '0.00', 'Viewsonic VA2231wa - 0173', 1),
(11, '0172', 1007, '1900-01-01', '0.00', 'Dell 19" Monitor 0172', 1),
(12, '00104926EC6B', 1008, '1900-01-01', '0.00', 'Shortel 230 - EC6B', 1),
(13, '0227', 1009, NULL, NULL, 'Blue - Yeti Mic', 0),
(14, '0221', 1, NULL, NULL, 'WS0221', 1),
(15, '0222', 1, '2013-11-29', '323.00', 'WS0222', 1),
(16, '0223', 1, NULL, NULL, 'WS0223', 1),
(17, '0220', 1, '2013-11-29', '323.00', 'WS0220', 1),
(18, '0217', 1, '2013-11-29', '323.00', 'WS0217', 1),
(19, '0218', 1, NULL, NULL, 'WS0218', 1),
(20, '0219', 1, '2013-11-29', '323.00', 'WS0219', 1),
(21, '0228', 2, NULL, NULL, 'Viewsonic VA2231wa - 0228', 1),
(22, '0229', 1010, NULL, NULL, 'Dell 19" Monitor 0229', 1),
(23, '00104931AA16', 1011, NULL, NULL, 'Shortel 115 - AA16', 1),
(24, '0093 - DYTJ18X4DJ8T', 1012, NULL, NULL, 'Office IPad 3', 1),
(25, '0095', 1013, '1900-01-01', '0.00', '320Gb External HDD', 1),
(26, '0071', 1014, NULL, NULL, '0071 - NEC Projector', 0),
(27, '0072', 1015, NULL, NULL, '0072 - Black Dell Projector', 0),
(28, '0073', 1016, '1900-01-01', '0.00', '0073 - Dell Projector', 0),
(29, '0230', 1017, '1900-01-01', '0.00', '0230 - Silver Dell Projector', 0),
(30, '0064', 1018, NULL, NULL, 'WS0064', 0),
(31, '0231', 1019, NULL, NULL, 'Freecom 1GB Pen - 0231', 1),
(47, '0165', 2, NULL, NULL, 'Viewsonic VA2231wa - 0165', 1),
(48, '0232', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0232', 1),
(49, '0233', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0233', 1),
(50, '0137', 1022, NULL, NULL, 'Viewsonic VA2248-LED - 0137', 1),
(51, '0234', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0234', 1),
(52, '0235', 1010, '1900-01-01', '0.00', 'Dell 19" Monitor 0235', 1),
(53, '0134', 1010, NULL, NULL, 'Dell 19" Monitor 0134', 0),
(54, '0135', 1022, NULL, NULL, 'Viewsonic VA2248-LED - 0135', 1),
(55, '0236', 3, '1900-01-01', '0.00', 'Samsung S24B150BL - 0236', 1),
(56, '001049201D9A', 1008, '1900-01-01', '0.00', 'Shortel 230 - 1D9A', 1),
(57, '0010492015AE', 1008, '1900-01-01', '0.00', 'Shortel 230 - 15AE', 1),
(93, '0269', 1029, '1900-01-01', '0.00', 'TP-Link Switch - 0269', 0),
(94, '0058', 1030, NULL, NULL, 'WS0058', 1),
(95, '0270', 1031, NULL, NULL, 'MeetingRoom3', 1),
(96, '0243', 1032, NULL, NULL, 'MeetingRoom2', 1),
(97, '0271', 1027, NULL, NULL, 'Dynamode SW80010-D Switch - 0271', 0),
(123, '0281', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0281', 0),
(124, '0282', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0282', 1),
(125, '0283', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0283', 0),
(126, '0284', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0284', 1),
(127, '0285', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0285', 1),
(128, '0286', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0286', 1),
(129, '0287', 1045, '2014-07-18', '104.50', 'Philips 23.6" Monitor 0287', 1),
(143, '0280', 1, '2014-07-03', '403.80', 'WS0280', 1),
(144, '0296', 1, '2014-07-03', '403.80', 'WS0296', 1),
(145, '0297', 1, '2014-07-03', '403.80', 'WS0297', 1),
(146, '0298', 1, '2014-07-03', '403.80', 'WS0298', 1),
(147, '0299', 1, '2014-07-03', '403.80', 'WS0299', 1),
(148, '0052', 1036, '1900-01-01', '0.00', 'WS0052', 1),
(168, '0312', 1047, NULL, NULL, 'Epson White HD - 0312', 1),
(169, '0201', 1049, '1900-01-01', '0.00', 'Ipad 4 - 0201', 0),
(170, 'HP27J-2C496-83KXB-RGMX6-8QJQG', 1020, '1900-01-01', '0.00', 'MS Office for Jonny D', 1),
(171, '7N4QY-DFGWD-P6662-CFCHG-QYFP2', 1021, '1900-01-01', '0.00', 'MS Office for WS0215 (Simeon Laptop)', 1),
(172, '0140', 1022, '1900-01-01', '0.00', 'Viewsonic VA2248-LED - 0140', 1),
(198, '0109', 1037, '1900-01-01', '0.00', 'WS0109', 1),
(199, '0324', 1052, '1900-01-01', '0.00', 'Philips 23.6" Monitor 0324', 1)
;
CREATE TABLE [Asset-User]
([ID] int, [Asset_ID] int, [User_ID] int, [Sign_Out_Date] datetime, [Return_Date] datetime, [Perm_Signout] int)
;
INSERT INTO [Asset-User]
([ID], [Asset_ID], [User_ID], [Sign_Out_Date], [Return_Date], [Perm_Signout])
VALUES
(2, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(3, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(4, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(5, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(6, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(7, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(8, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(9, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(10, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(11, 1, 1, '2014-03-29 00:00:00', '2014-03-29 00:00:00', 0),
(1002, 1, 1, '2014-04-01 00:00:00', '2014-04-01 00:00:00', 0),
(1003, 1, 1, '2014-01-01 00:00:00', '2014-01-01 00:00:00', 1)
;
CREATE TABLE Company_Users
([ID] int, [Name] varchar(14), [Domain_Username] varchar(14), [Dept] varchar(16), [Email] varchar(25), [DD_Ext] int, [Job_Title] varchar(30), [Deleted_Left] int)
;
INSERT INTO Company_Users
([ID], [Name], [Domain_Username], [Dept], [Email], [DD_Ext], [Job_Title], [Deleted_Left])
VALUES
(1, 'Neil Smithson', 'Neil.Smithson', '2nd Line Support', 'neil.smithson#dezrez.com', 3041, 'Second Line Support Technician', 0)
;
CREATE TABLE Global_Types
([ID] int, [Name] varchar(13), [Description] varchar(54))
;
INSERT INTO Global_Types
([ID], [Name], [Description])
VALUES
(1, 'PC', 'Desktop PC')
;
This is not really an answer but will show you how you could rework this query using aliases and the (not really) newer join style.
SELECT au.ID
, au.Asset_ID
, a.ID
, a.Signed_Out
, a.Asset_Tag_Serial_Num
, a.Name_Hostname
, a.Type_ID
, at.ID
, at.Name_Model
, au.User_ID
, cu.ID
, au.Sign_Out_Date
, au.Return_Date
, au.[Perm_Signout?]
FROM [Asset-User] au
join Assets a on au.Asset_ID = a.ID
join Asset_Type at on a.Type_ID = at.ID
join Company_Users cu on au.User_ID = cu.ID
WHERE au.User_ID = '1'
AND a.Signed_Out = '1'

WITH keyword is required

While running the following SQL query -
INSERT INTO Countries ('sno', 'Name' ) VALUES
(1, 'Afghanistan'),
(2, 'Albania'),
(3, 'Algeria'),
(4, 'American Samoa'),
(5, 'Andorra'),
(6, 'Angola'),
(7, 'Anguilla'),
(8, 'Antarctica'),
(9, 'Antigua and Barbuda'),
(10, 'Argentina'),
(11, 'Armenia'),
(12, 'Armenia'),
(13, 'Aruba'),
(14, 'Australia'),
(15, 'Austria'),
(16, 'Azerbaijan'),
(17, 'Azerbaijan'),
(18, 'Bahamas'),
(19, 'Bahrain'),
(20, 'Bangladesh'),
(21, 'Barbados'),
(22, 'Belarus'),
(23, 'Belgium'),
(24, 'Belize'),
(25, 'Benin'),
(26, 'Bermuda'),
(27, 'Bhutan'),
(28, 'Bolivia'),
(29, 'Bosnia and Herzegovina'),
(30, 'Botswana'),
(31, 'Bouvet Island'),
(32, 'Brazil'),
(33, 'British Indian Ocean Territory'),
(34, 'Brunei Darussalam'),
(35, 'Bulgaria'),
(36, 'Burkina Faso'),
(37, 'Burundi'),
(38, 'Cambodia'),
(39, 'Cameroon'),
(40, 'Canada'),
(41, 'Cape Verde'),
(42, 'Cayman Islands'),
(43, 'Central African Republic'),
(44, 'Chad'),
(45, 'Chile'),
(46, 'China'),
(47, 'Christmas Island'),
(48, 'Cocos (Keeling) Islands'),
(49, 'Colombia'),
(50, 'Comoros'),
(51, 'Congo'),
(52, 'Congo, The Democratic Republic of The'),
(53, 'Cook Islands'),
(54, 'Costa Rica'),
(55, 'Cote Divoire'),
(56, 'Croatia'),
(57, 'Cuba'),
(58, 'Cyprus'),
(59, 'Cyprus'),
(60, 'Czech Republic'),
(61, 'Denmark'),
(62, 'Djibouti'),
(63, 'Dominica'),
(64, 'Dominican Republic'),
(65, 'Easter Island'),
(66, 'Ecuador'),
(67, 'Egypt'),
(68, 'El Salvador'),
(69, 'Equatorial Guinea'),
(70, 'Eritrea'),
(71, 'Estonia'),
(72, 'Ethiopia'),
(73, 'Falkland Islands (Malvinas)'),
(74, 'Faroe Islands'),
(75, 'Fiji'),
(76, 'Finland'),
(77, 'France'),
(78, 'French Guiana'),
(79, 'French Polynesia'),
(80, 'French Southern Territories'),
(81, 'Gabon'),
(82, 'Gambia'),
(83, 'Georgia'),
(84, 'Georgia'),
(85, 'Germany'),
(86, 'Ghana'),
(87, 'Gibraltar'),
(88, 'Greece'),
(89, 'Greenland'),
(90, 'Greenland'),
(91, 'Grenada'),
(92, 'Guadeloupe'),
(93, 'Guam'),
(94, 'Guatemala'),
(95, 'Guinea'),
(96, 'Guinea-bissau'),
(97, 'Guyana'),
(98, 'Haiti'),
(99, 'Heard Island and Mcdonald Islands'),
(100, 'Honduras'),
(101, 'Hong Kong'),
(102, 'Hungary'),
(103, 'Iceland'),
(104, 'India'),
(105, 'Indonesia'),
(106, 'Indonesia'),
(107, 'Iran'),
(108, 'Iraq'),
(109, 'Ireland'),
(110, 'Israel'),
(111, 'Italy'),
(112, 'Jamaica'),
(113, 'Japan'),
(114, 'Jordan'),
(115, 'Kazakhstan'),
(116, 'Kazakhstan'),
(117, 'Kenya'),
(118, 'Kiribati'),
(119, 'Korea, North'),
(120, 'Korea, South'),
(121, 'Kosovo'),
(122, 'Kuwait'),
(123, 'Kyrgyzstan'),
(124, 'Laos'),
(125, 'Latvia'),
(126, 'Lebanon'),
(127, 'Lesotho'),
(128, 'Liberia'),
(129, 'Libyan Arab Jamahiriya'),
(130, 'Liechtenstein'),
(131, 'Lithuania'),
(132, 'Luxembourg'),
(133, 'Macau'),
(134, 'Macedonia'),
(135, 'Madagascar'),
(136, 'Malawi'),
(137, 'Malaysia'),
(138, 'Maldives'),
(139, 'Mali'),
(140, 'Malta'),
(141, 'Marshall Islands'),
(142, 'Martinique'),
(143, 'Mauritania'),
(144, 'Mauritius'),
(145, 'Mayotte'),
(146, 'Mexico'),
(147, 'Micronesia, Federated States of'),
(148, 'Moldova, Republic of'),
(149, 'Monaco'),
(150, 'Mongolia'),
(151, 'Montenegro'),
(152, 'Montserrat'),
(153, 'Morocco'),
(154, 'Mozambique'),
(155, 'Myanmar'),
(156, 'Namibia'),
(157, 'Nauru'),
(158, 'Nepal'),
(159, 'Netherlands'),
(160, 'Netherlands Antilles'),
(161, 'New Caledonia'),
(162, 'New Zealand'),
(163, 'Nicaragua'),
(164, 'Niger'),
(165, 'Nigeria'),
(166, 'Niue'),
(167, 'Norfolk Island'),
(168, 'Northern Mariana Islands'),
(169, 'Norway'),
(170, 'Oman'),
(171, 'Pakistan'),
(172, 'Palau'),
(173, 'Palestinian Territory'),
(174, 'Panama'),
(175, 'Papua New Guinea'),
(176, 'Paraguay'),
(177, 'Peru'),
(178, 'Philippines'),
(179, 'Pitcairn'),
(180, 'Poland'),
(181, 'Portugal'),
(182, 'Puerto Rico'),
(183, 'Qatar'),
(184, 'Reunion'),
(185, 'Romania'),
(186, 'Russia'),
(187, 'Russia'),
(188, 'Rwanda'),
(189, 'Saint Helena'),
(190, 'Saint Kitts and Nevis'),
(191, 'Saint Lucia'),
(192, 'Saint Pierre and Miquelon'),
(193, 'Saint Vincent and The Grenadines'),
(194, 'Samoa'),
(195, 'San Marino'),
(196, 'Sao Tome and Principe'),
(197, 'Saudi Arabia'),
(198, 'Senegal'),
(199, 'Serbia and Montenegro'),
(200, 'Seychelles'),
(201, 'Sierra Leone'),
(202, 'Singapore'),
(203, 'Slovakia'),
(204, 'Slovenia'),
(205, 'Solomon Islands'),
(206, 'Somalia'),
(207, 'South Africa'),
(208, 'South Georgia and The South Sandwich Islands'),
(209, 'Spain'),
(210, 'Sri Lanka'),
(211, 'Sudan'),
(212, 'Suriname'),
(213, 'Svalbard and Jan Mayen'),
(214, 'Swaziland'),
(215, 'Sweden'),
(216, 'Switzerland'),
(217, 'Syria'),
(218, 'Taiwan'),
(219, 'Tajikistan'),
(220, 'Tanzania, United Republic of'),
(221, 'Thailand'),
(222, 'Timor-leste'),
(223, 'Togo'),
(224, 'Tokelau'),
(225, 'Tonga'),
(226, 'Trinidad and Tobago'),
(227, 'Tunisia'),
(228, 'Turkey'),
(229, 'Turkey'),
(230, 'Turkmenistan'),
(231, 'Turks and Caicos Islands'),
(232, 'Tuvalu'),
(233, 'Uganda'),
(234, 'Ukraine'),
(235, 'United Arab Emirates'),
(236, 'United Kingdom'),
(237, 'United States'),
(238, 'United States Minor Outlying Islands'),
(239, 'Uruguay'),
(240, 'Uzbekistan'),
(241, 'Vanuatu'),
(242, 'Vatican City'),
(243, 'Venezuela'),
(244, 'Vietnam'),
(245, 'Virgin Islands, British'),
(246, 'Virgin Islands, U.S.'),
(247, 'Wallis and Futuna'),
(248, 'Western Sahara'),
(249, 'Yemen'),
(250, 'Yemen'),
(251, 'Zambia'),
(252, 'Zimbabwe');
following error code is seen on microsoft sql server-
Msg 215, Level 16, State 1, Line 1
Parameters supplied for object 'Countries' which is not a function. If the parameters are intended as a table hint, a WITH keyword is required.
Please help by sharing the reason ?
Don't use single quotes for column name.
INSERT INTO Countries ([sno], [Name])...
You can use brackets normally.
Or you can use double quote " if SET QUOTED_IDENTIFIER is ON (it should be by default)
Currently you have string literals which is causing SQL Server to interpret the code way different to what you expect
You could leave all the bracketed section out all together: SQL Fiddle
INSERT INTO Countries VALUES
This is therefore none explicit and not best practice (see comment by #AaronBertrand) - but still an alternative.