SQL rollup two columns error - sql

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

Related

how to insert values through select in ssms ? and i am getting error through which i tried

I tried inserting values into a table that I created but while inserting into them through select statement I am getting an error message
the code which I tried for insert is
create table dbo.watermarktable
(tablename varchar(255),
watermarkvalue datetime,
);
insert into dbo.watermarktable(tablename,watermarkvalue)
("reference_value", select max(created_date) from dbo.reference_value_genc);
but the values were not taken and thrown an error message
and the error message is
Incorrect syntax near 'reference_value'.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
Completion time: 2021-10-28T14:29:43.2357252+01:00
You can either use VALUES or SELECT with INSERT INTO. So Change your insert like this
insert into dbo.watermarktable(tablename,watermarkvalue)
SELECT 'reference_value', max(created_date) from dbo.reference_value_genc;
Your syntax is a bit off, change it to this:
INSERT INTO dbo.watermarktable (tablename,watermarkvalue)
SELECT 'reference_value', max(created_date)
FROM dbo.reference_value_genc;

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

Invalid column name for table type parameter

I have a table type which is used in a stored procedure to filter out values.
CREATE TYPE PackageIdType AS TABLE
(
PackageId VARCHAR(150)
);
My stored procedure is this:
CREATE PROCEDURE [dbo].[spLocalGetValuesFromTable]
#RundateStart datetime,
#RundateEnd datetime,
#CreationIds PackageIdType READONLY
AS
SELECT *
FROM MYTABLE
WHERE date BETWEEN #RundateStart AND #RundateEnd
AND Ids IN (#CreationIds)
But when I run this getting error:
Msg 207, Level 16, State 1, Procedure spLocalGetValuesFromTable, Line --[Batch Start Line 0]
Invalid column name '#CreationIds'
It's a table, so the correct syntax would be
SELECT *
FROM MYTABLE
WHERE date Between #RundateStart And #RundateEnd
AND Ids in (SELECT PackageId FROM #CreationIds)
This assumes that each row in the table #CreationIds is an Id that can map to the same type as Ids in MYTABLE.

String or binary data would be truncated in asp.net

I'm getting String or binary data would be truncated error when, I'm trying to execute
Insert into Student_info (StudentId,FirstName,LastName,DOB,StudentAddress,StudentEmail,StudentMobile)
(Select StudentId,FirstName,LastName,DOB,Address,EmailId,Mobile from Student_temp where StudentId='" & studentid & "')
Table structure of Student_Temp
Table structure of Student_Info
Need Help !!
This error is reported by SQL Server when you try and insert string or binary data into a column which doesn't have enough width to hold it, e.g.
create table MyTable
(
Column1 VARCHAR(10)
)
insert into MyTable (Column1) VALUES ('1234567890A')
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated
At a guess, it is because your Student_info.StudentMobile is varchar(10) whereas Student_temp.Mobile is varchar(50)

OUPUT for insert from table variable gives: "The multi-part identifier "k.CustomerName" could not be bound."

I have seen many people here on stack overflow with this error message and all get it in another situation. I could not find my own situation among the already existing questions. So I hope someone can help me with this. I use SQL server 9 with SQL management studio 10.
--import the customers.
CREATE TABLE #AllCustomers(CustomerName NVARCHAR(100), CustomerNr NVARCHAR(16));
BULK INSERT #AllCustomers
FROM 'C:\allcustomers.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
DECLARE #DefinitionId int;
--get the id of the definition for which I have to set values.
SELECT #DefinitionId = pkDefinitionId
FROM dbo.Definitions
WHERE Name = 'DEFINITION-OF-MY-ITEM';
DECLARE #TempA TABLE (CustomerName NVARCHAR(255), CustomerNr NVARCHAR(16));
--reduce the set of all customers to only the customer for whom I have to insert.
WITH MyView AS
(SELECT kciv.CustomerNr
FROM dbo.CustomerItems kciv
INNER JOIN dbo.DefinitionToItem civ ON civ.pkDefinitionToItemId = kciv.pkCustomerItemId
WHERE civ.fkDefinitionId = #DefinitionId)
INSERT INTO #TempA
SELECT k.CustomerName , k.CustomerNr
FROM #AllCustomers k
WHERE k.CustomerNr NOT IN (SELECT CustomerNr FROM MyView);
--used to store the generated primairy keys I need for creating relations.
DECLARE #ItemIds TABLE (CustomerName NVARCHAR(255), CustomerNr NVARCHAR(16), pkItemId int);
DECLARE #DefinitionToItemIds TABLE (CustomerName NVARCHAR(255), CustomerNr NVARCHAR(16), pkDefinitionToItemId int);
--insert the default values.
INSERT INTO dbo.Items
OUTPUT k.CustomerName, k.CustomerNr, inserted.pkItemId
INTO #ItemIds (CustomerName, CustomerNr, pkGenericValueId)
SELECT 2, 1, null, null, 1, null
FROM #TempA k;
--couple the values to the definition.
INSERT INTO dbo.DefinitionToItem
OUTPUT gvd.CustomerName, gvd.CustomerNr, inserted.pkDefinitionToItemId
INTO #DefinitionToItemIds
SELECT 1, 0, #DefinitionId, gvd.pkItemId
FROM #ItemIds gvd;
--couple the 'coupling' to the customers.
INSERT INTO dbo.CustomerItems
SELECT civd.pkDefinitionToItemId, civd.CustomerName, civd.CustomerNr
FROM #DefinitionToItemIds civd;
I get four errors when running the query, all on the two output lines near the end of the code sample.
Msg 4104, Level 16, State 1, Line 64 The multi-part identifier "k.CustomerName" could not be bound.
Msg 4104, Level 16, State 1, Line 64
The multi-part identifier "k.CustomerNr" could not be bound.
Msg 4104, Level 16, State 1, Line 69
The multi-part identifier "gvd.CustomerName" could not be bound.
Msg 4104, Level 16, State 1, Line 69
The multi-part identifier "gvd.CustomerNr" could not be bound.
I have checked for typos but couldn't find any (I might have introduced some here though while changing some of the names to remove the context). I can't find out why this is going wrong. I've looked at MSDN, but I can't find anything wrong.
Extra info:
The database schema is as follows:
The Items table contains "values" (pkItemId, bunch of other columns)
The Definition table contains "definitions" (pkDefinitionId, Name, bunch of other columns)
The DefinitionToItem table matches the values to definitions (pkDefinitionToItemId, fkDefinitionId, fkItemId)
The CustomerItems table links a customer to a DefinitionToItemId (pkDefinitionToItemId, CustomerName, CustomerNr).
What I need to achieve is to insert default values (i.e. "2, 1, null, null, 1, null" linked to definitino 'DEFINITION-OF-MY-ITEM') into the items database for a given set of customers. Some might already have a value for that definition and then I should skip them (hence the #TempA).
So I insert the value into Items, then insert the coupling between definition and items in DefinitionToItem and lastly couple the customer to the DefinitionToItem by inserting into the DefinitionToItem table.
If there if a better way to achieve this than through what I'm doing, then I'm open to suggestions.
I think the approach you are taking is complicating the scenario.
Firstly, the OUTPUT clause is not going to work the way you need it to work here, because you can only use columns inserted, and you want to use columns from the source table, but ones that does not get inserted. (CustomerName as an example).
There are two ways I suggest you can go about this:
First Approach. Change you query. Use OUTPUT, but output an id field. Then, after your first insert into ITEMS, join your new table with your source table. NOW you will have access to all fields and still a way to identify which records should be inserted.
Second approach. Drop your temp tables. Use a simple insert statement with a WHERE _ NOT IN clause. This takes away the complexity and still achieves the goal of not inserting duplicates, just on a closer level.
Use:
INSERT INTO dbo.Items
OUTPUT INSERTED.CustomerName, INSERTED.CustomerNr, INSERTED.pkItemId
INTO #ItemIds (CustomerName, CustomerNr, pkGenericValueId)
SELECT 2, 1, null, null, 1, null
FROM #TempA k;
INSERT INTO dbo.DefinitionToItem
OUTPUT INSERTED.CustomerName, INSERTED.CustomerNr, INSERTED.pkDefinitionToItemId
INTO #DefinitionToItemIds
SELECT 1, 0, #DefinitionId, gvd.pkItemId
FROM #ItemIds gvd;
You might need to change the column names, because they need to come from the table being inserted into rather than the column they were sourced from.
Your table #AllCustomers does not contain field CustomerName in this query
SELECT k.CustomerName , k.CustomerNr
FROM #AllCustomers k