SQL Server 2005 Issue Column name or number of supplied values does not match table definition - sql

I wish to DELETE the data from a table before performing an INSERT INTO, however I keep recieving an error stating:
Insert Error: Column name or number of supplied values does not match table definition.
I've also tried defining the columns the data should be entered into as part of the INSERT INTO statement, but then get issues with column names, even though they are correct. I have a feeling the issues relates to me selecting 2 PostCode entries and converting them into 1, but if someone could shed light on this it would be a big help.
My code can be found below, if you want me to add the code where I was sepcifing column names let me know. So you know the fields selected are all the fields in the Course table other than AutoNum which is a auto number primary key and SSMA_TimeStamp, which is a TimeStamp.
BEGIN
DELETE dbo.Course
INSERT INTO dbo.Course
SELECT
RTRIM( CAST (sd.[RefNo] AS nvarchar(50))) AS 'Student Ref No',
sd.[FirstForeName] AS Forename,
sd.[Surname],
sd.[Address1],
sd.[Address2],
sd.[Address3],
sd.[Address4],
sd.[DateOfBirth] AS DOB,
sd.[PostCodeOut] + ' ' + sd.[PostCodeIn] AS 'Post Code',
o.[Name] AS 'Course Name',
o.[Code] As 'Course Code',
e.[StartDate] AS 'Start Date',
e.[ExpectedGLH] AS 'Exp GLH',
e.[ExpectedEndDate] AS 'Expected End Date',
e.[ActualEndDate] AS 'Actual End Date',
e.[Grade] AS 'Grade',
ou.[Description] AS Outcome,
cs.[Description] AS 'Completion Status',
sd.[Tel1] AS 'Tel 1'
FROM [xxxxxxx].[xxxxxx].[dbo].[StudentDetail] sd
INNER JOIN [xxxxxxx].[xxxxxx].[dbo].[Enrolment] e
ON sd.[StudentDetailID] = e.[StudentDetailID]
Inner JOIN [xxxxxxx].[xxxxxx].[dbo].[Offering] o
ON o.[OfferingID] = e.[OfferingID]
INNER JOIN [xxxxxxx].[xxxxxx].[dbo].[CompletionStatus] cs
ON cs.[CompletionStatusID] = e.[CompletionStatusID]
INNER JOIN [xxxxxxx].[xxxxxx].[dbo].[Outcome] ou
ON ou.[OutcomeID] = e.[OutcomeID]
WHERE sd.[AcademicYearID] = '09/10'
AND
o.[Code] LIKE '%-ee%'
AND
o.[Name] LIKE '%-%dl%'
ORDER BY
sd.[RefNo]

It sounds like your 'Course' table does not match your insert statement, either in the number or names of the columns specified (as per the error message).
Could you add the create table code for the 'Course' table as that will show where the discrepancy lies.
Thanks.

I would explicitly list the columns in the course table that you are inserting into - this may solve your problem/help find your issue, but also reduce maintenance problems in the future.

To fix this issue you need explicitly specify list of the table's columns in the INSERT INTO statement.

you should add a list of columns to the INSERT statement, see below, where you explicitly list each column from dbo.Course that you intend to populate in your INSERT:
INSERT INTO dbo.Course
---<<<<<
(col1, col2, col3, col4, clo5....) ---<<<<<Add this here
---<<<<<
SELECT
RTRIM( CAST (sd.[RefNo] AS nvarchar(50))) AS 'Student Ref No',
sd.[FirstForeName] AS Forename,
sd.[Surname],
sd.[Address1],
sd.[Address2],
sd.[Address3],
sd.[Address4],
sd.[DateOfBirth] AS DOB,
sd.[PostCodeOut] + ' ' + sd.[PostCodeIn] AS 'Post Code',
o.[Name] AS 'Course Name',
o.[Code] As 'Course Code',
e.[StartDate] AS 'Start Date',
e.[ExpectedGLH] AS 'Exp GLH',
e.[ExpectedEndDate] AS 'Expected End Date',
e.[ActualEndDate] AS 'Actual End Date',
e.[Grade] AS 'Grade',
ou.[Description] AS Outcome,
cs.[Description] AS 'Completion Status',
sd.[Tel1] AS 'Tel 1'
FROM ....
then make sure that each column in the SELECT list matches each of these columns and in order. From your error, it sounds like you have too many any or too few returned columns in the SELECT.

Related

How can I avoid "stringly typed" code in T-SQL?

Consider some code like
SELECT
CASE
WHEN [DECISION-MAKER] = 'COKE' THEN 'GIVE COKE'
WHEN [DECISION-MAKER] IN ('PEPSI', 'BLOOD') THEN 'GIVE DEATH'
END AS [EMPLOYEE-ASSIGNMENT],
CASE [DECISION-MAKER]
WHEN 'COKE' THEN 'Employee prefers coke. Give coke.'
WHEN 'PEPSI' THEN 'Employee prefers pepsi. Give death.'
WHEN 'BLOOD' THEN 'Employee is some sort of vampire. Give death.'
END AS [ASSIGNMENT-REASON]
FROM
(
SELECT *,
CASE
WHEN [COMPLEX-LOGIC-1] THEN 'COKE'
WHEN [COMPLEX-LOGIC-2] THEN 'PEPSI'
WHEN [COMPLEX-LOGIC-3] THEN 'BLOOD'
END AS [DECISION-MAKER]
FROM [WHEREVER]
)
I believe that the technical term for such poor code is "stringly typed". The key issue in the above code is that decisions are being made based on a string output that the developer needs to type and consistently get correct. If anything goes wrong, the language will be incapable of throwing errors. In a traditional language, the workaround would be to construct some sort of dictionary to handle these cases. What is the idiomatic solution in T-SQL? I don't like the idea of making a use-once table, but maybe a temp table would be helpful?
I wouldn't worry about having a "Use in one place" table. If you still worry about it cluttering up your database, you can use a table variable.
DECLARE #AssignmentAndReason AS TABLE (
DECISION-MAKER varchar,
EMPLOYEE-ASSIGNMENT varchar,
ASSIGNMENT-REASON varchar
);
INSERT INTO #AssignmentAndReason VALUES
('COKE', 'GIVE COKE', 'Employee prefers coke. Give coke.'),
('PEPSI', 'GIVE DEATH', 'Employee prefers pepsi. Give death.'),
('BLOOD', 'GIVE DEATH', 'Employee is some sort of vampire. Give death.');
SELECT [EMPLOYEE-ASSIGNMENT], [ASSIGNMENT-REASON],
FROM [WHEREVER]
JOIN #AssignmentAndReason ON [DECISION-MAKER] = CASE
WHEN [COMPLEX-LOGIC-1] THEN 'COKE'
WHEN [COMPLEX-LOGIC-2] THEN 'PEPSI'
WHEN [COMPLEX-LOGIC-3] THEN 'BLOOD'
END
uses "with as(query)"
with firstLogic as
(
select PersonID, Name,
CASE
WHEN valueDRINK=1 or valueDRINK=2 THEN 'COKE'
WHEN valueDRINK=3 or valueDRINK=4 THEN 'PEPSI'
WHEN valueDRINK=5 THEN 'BLOOD'
END AS [DECISION-MAKER]
from Persons
where country='co'
)
select PersonID, Name ,[DECISION-MAKER],
CASE
WHEN [DECISION-MAKER] = 'COKE' THEN 'GIVE COKE'
WHEN [DECISION-MAKER] IN ('PEPSI', 'BLOOD') THEN 'GIVE DEATH'
END AS [EMPLOYEE-ASSIGNMENT],
CASE [DECISION-MAKER]
WHEN 'COKE' THEN 'Employee prefers coke. Give coke.'
WHEN 'PEPSI' THEN 'Employee prefers pepsi. Give death.'
WHEN 'BLOOD' THEN 'Employee is some sort of vampire. Give death.'
END AS [ASSIGNMENT-REASON]
from firstLogic
"with" allows you to have different queries and then join them without creating a temporary table
you can see it in this example

SQL: Consolidating results

I am a relative beginner in SQL (Learned and forgotten many times) and entirely self taught so please excuse my likely lack of proper terminology. I made a query that pulls items that have been returned, each items' row has a return code. Here is a result sample:
In the final report(Created with Visual Studio), I would like to be able to have a count of returns by return type but I would need to consolidate the 40 or so return codes into 4 or 5 return type groups. So RET_CODE values ) and . are both product quality issues and would count in the "Product Quality" row.
I could use some help with what the best way to accomplish this would be.
Thank You
Andrew
The bad way!
You could do this by creating the grouping within your SQL statement with something like
SELECT
*,
CASE
WHEN RET_CODE IN ('.', ')') THEN 'Quality Error'
WHEN RET_CODE IN ('X', 'Y', 'Z') THEN 'Some Other error'
ELSE [Desc1]
END AS GroupDescription
FROM myTable
The problem with this approach is that you have to keep repeating it every time you want to something similar.
The better option. (but not perfect!)
Assuming you do not already have such a table...
Create a table that contains the grouping. You can use this in the future whenever you need to do this kind of thing.
For example.
CREATE TABLE dbo.MyErrorGroupTable (RET_CODE varchar(10), GroupDescription varchar(50))
INSERT INTO dbo.MyErrorGroupTable VALUES
('.', 'Quality Error'),
(')', 'Quality Error'),
('X', 'Some Other Error'),
('Y', 'Some Other Error'),
('.', 'Some Other Error'),
('P', 'UPS Error'),
('A', 'PAck/Pick Error')
etc....
Then you can simply join to this table and use the GroupDescription in your report
e.g.
SELECT
a.*, b.GroupDescription
FROM myTable a
JOIN MyErrorGroupTable b ON a.RET_CODE = b.RET_CODE
Hope this helps...
You're looking for the GROUP BY clause.

iis invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I'm currently trying to add another table in here "dbo.ARTICLE_INCARNATION.article_incarnation_id", but I keep getting the error:
iis invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I have tried what i know to fix it but no progress.
SELECT
'Summary',SUM( OBJ#CAB.object_qty) AS 'Sum est, OBW exist',
AIN.article_incarnation_long_no AS 'Article no',
AIN.article_incarnation_name AS 'Article name',
SUM( OBJ#CAB.object_qty) AS 'Sum est, OBW exist',
(SELECT
isnull(sum(POI.purchase_order_item_qty_po),0)
FROM
[dbo].[PURCHASE_ORDER_ITEM] POI
WHERE
POI.article_incarnation_id = AIN.article_incarnation_id
AND POI.purchase_order_id IS NOT NULL),
(SELECT sum(isnull(stock_object_qty_qty,0)) FROM [dbo].[STOCK_OBJECT_QTY] S WHERE S.article_incarnation_id = OBJ#CAB.article_incarnation_id) AS 'Total on stock'
/*AS 'Manko'
AS 'PO',
AS 'Not deliverd PO'*/
--skalmed
FROM
OBJECT OBJ#CAB
JOIN OBJECT_TYPE OTY#CAB ON OTY#CAB.object_type_id = OBJ#CAB.object_type_id
AND OTY#CAB.object_type_no = 'CABLE'
JOIN [dbo].[ARTICLE_INCARNATION] AIN ON OBJ#CAB.article_incarnation_id = AIN.article_incarnation_id
AND AIN.article_unit_no = 'M'
JOIN OBJECT_WORK OBW#CAB ON OBW#CAB.object_id = OBJ#CAB.object_id
AND OBW#CAB.work_type_no IN ('EPU', 'EPA', 'IPA', 'XEPU', 'LI5', 'LE5')
AND OBW#CAB.object_work_installed_qty IS NULL
GROUP BY
AIN.article_incarnation_long_no,
AIN.article_incarnation_name,
ARTICLE_INCARNATION.article_incarnation_id,
OBJ#CAB.article_incarnation_id
You have same table name but schemas are different that cause the problem. :
ARTICLE_INCARNATION.article_incarnation_id
AND
[dbo].[ARTICLE_INCARNATION]
Choose which one you want to aggregate or display. If you want both then define proper alias.

Retrieve Duplicate Names (not numbers) in a Table

I am going to attach a link that showcases exactly what I am facing, with the code shown and descriptions of what should be happeneing.
See link: https://www.screencast.com/t/Zo3BH0i3v
In the event that you do not want to open that link, below I will explain the issue:
First and foremost, the code:
--Query Duplicate Document Names--
SELECT count(dcd.Name)'How Many Duplicates', dcf.Name 'Folder Name', dcf.ID
'Folder ID', dcd.ID 'Document ID', dcd.name 'Document Name',
'https://' + txtcityssldomainname + '/' + 'Admin/DocumentCenter/Folder/Index/'
+ Cast (FK_FolderID AS VARCHAR) AS Link_To_Folder
FROM tblcitysettings tblcs, DocumentCenterDocuments dcd
JOIN DocumentCenterFolders dcf on dcf.ID = dcd.FK_FolderID
WHERE dcf.FK_Status IN (40,10) AND (dcd.FK_Status IN (40,10)) AND
(dcd.IsArchived IN (0)) AND (dcf.Name NOT IN ('Content', 'Design', 'Banners',
'MyAccount'))
GROUP BY dcf.Name, dcf.ID, dcd.ID, dcd.Name, 'https://' + txtcityssldomainname
+ '/' + 'Admin/DocumentCenter/Folder/Index/' + Cast (FK_FolderID AS VARCHAR)
HAVING count(dcd.Name) = 1
ORDER BY dcd.ID
What should happen, is it should return a count in the first column of "1" for every document that appears once. If the document appears more than once, it will concatenate those rows and show a value of how many appeared.
It is not working and I am completely at a loss.
Any help would be greatly appreciated!
Below is the Edited, Working copy:
Note: I had realized that I was trying to merge a singular value in a column when there were other values that were not the same. Therefore, how could it merge those strings? I re-wrote the code to display the bare minimum.
SELECT count(dcd.DisplayName) 'Number of Duplicate Entries', dcd.DisplayName 'Document Name'
FROM DocumentCenterDocuments dcd
JOIN DocumentCenterFolders dcf on dcf.ID = dcd.FK_FolderID
where dcd.IsArchived IN (0) AND (dcd.FK_Status IN (40, 10)) AND (dcf.FK_Status IN (40, 10)) AND (dcf.Name NOT IN ('Content', 'Design', 'Banners', 'MyAccount'))
GROUP BY dcd.DisplayName
Having count(dcd.DisplayName) > 1
Thank you!

How to get Column Data types of particular table in SSAS using MDX

I have a query where I'm getting all tables and column names, but how to get Data Types and length values of columns of particular table?
SELECT [CATALOG_NAME] as [DATABASE],
CUBE_NAME AS [CUBE],[DIMENSION_UNIQUE_NAME] AS [DIMENSION],
LEVEL_CAPTION AS [ATTRIBUTE],
[LEVEL_NAME_SQL_COLUMN_NAME] AS [ATTRIBUTE_NAME_SQL_COLUMN_NAME],
[LEVEL_KEY_SQL_COLUMN_NAME] AS [ATTRIBUTE_KEY_SQL_COLUMN_NAME]
FROM $system.MDSchema_levels
WHERE CUBE_NAME ='Adventure Works'
AND level_origin=2
AND LEVEL_NAME <> '(All)'
order by [DIMENSION_UNIQUE_NAME]
Your friend is $SYSTEM.MDSCHEMA_PROPERTIES.
The query would probably be somewhat like :
select [HIERARCHY_UNIQUE_NAME], [LEVEL_UNIQUE_NAME], DATA_TYPE
from $SYSTEM.MDSCHEMA_PROPERTIES
where [Dimension_Unique_Name] = '[Accident Cause]'
and [Property_Name] = 'MEMBER_VALUE'
and [CUBE_NAME] = 'Adventure Works'
To understand what the values in column DATA_TYPE mean, refer this msdn link.