SQL Unpivot Conflicting columns name error - sql

I have 'book value' and 'Asset type' columns with set asset type values - 'NCD','GSEC' and 'EQU' .
I want to use unpivot and convert them from rows to cols using UNPIVOT but i am getting errors as follows.
Msg 207, Level 16, State 1, Line 267
Invalid column name 'GSEC'.
Msg 207, Level 16, State 1, Line 267
Invalid column name 'EQU'.
Msg 265, Level 16, State 1, Line 267
The column name "Book Value" specified in the UNPIVOT operator conflicts with the existing column name in the UNPIVOT argument.
Msg 265, Level 16, State 1, Line 267
The column name "Asset Type" specified in the UNPIVOT operator conflicts with the existing column name in the UNPIVOT argument.
Msg 8156, Level 16, State 1, Line 267
The column 'Book Value' was specified multiple times for 'UNPVT'.
This is the code snippet i am using
Select [Book Value],[Asset Type]
FROM [DBname].[dbo].[Final Base Data-4]
UNPIVOT([Book Value] For [Asset Type]
in ([NCD],[GSEC],[EQU])) AS UNPVT;

Related

I am trying to copy from one column to another in same table but getting msg 50000 and error 3609 as well

UPDATE poitem_mst
SET Uf_TYRI_poitem_req_ship_date=promise_date
Should be simple, but I get these errors:
Msg 50000, Level 16, State 1, Procedure RaiseErrorSp, Line 121 [Batch Start Line 0]
Purchase Order that has [PO: P000010685] does not exist.
Msg 3609, Level 16, State 1, Line 1
The transaction ended in the trigger. The batch has been aborted.
The PO number changes every time I run that query.

Stored procedure for inserting the new records in the table from data table

I am getting an error while creating the below stored-procedure which would insert the new records in the table from the data table
CREATE PROCEDURE [dbo].[SP_Cde_SecureList_Upsert]
#SecureList SecureList READONLY,
#ListType varchar(20)
AS
BEGIN
IF(#ListType='FETCHDETAILS')
SELECT * FROM TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST
ELSE
BEGIN
INSERT INTO TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST([CardRefID], [KEY], [MIS_Field1], [MIS_Field2], [ValidatingCarrier]
, [OtherCarriers], [Card Vendor], [Card], [Expiration], [Precedence], [AlertEmail], [maxTktAmt], [IRP_remark])
SELECT [CardRefID], [KEY], [MIS_Field1], [MIS_Field2], [ValidatingCarrier]
, [OtherCarriers], [Card Vendor], [Card], [Expiration], [Precedence], [AlertEmail], [maxTktAmt], [IRP_remark] FROM #SecureList;
END
END
GO
Error:
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'CardRefID'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'MIS_Field1'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'MIS_Field2'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 12 [Batch Start Line 60]
Invalid column name 'ValidatingCarrier'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'OtherCarriers'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Card Vendor'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Card'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Expiration'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'Precedence'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'AlertEmail'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'maxTktAmt'.
Msg 207, Level 16, State 1, Procedure SP_Cde_SecureList_Upsert, Line 13 [Batch Start Line 60]
Invalid column name 'IRP_remark'.
Completion time
which is strange since the column names are correct
Below is the screenshot of my SP and header/columns of the table wherein I am trying to insert the data from the data table
SP and table column details screenshot
If I do select * from the data table
CREATE PROCEDURE [dbo].[SP_Cde_SecureList_Upsert]
#SecureList SecureList READONLY,
#ListType varchar(20)
AS
BEGIN
IF(#ListType='FETCHDETAILS')
SELECT * FROM [dbo].[TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST]
ELSE
BEGIN
INSERT INTO [dbo].[TBL_TICKETING_ALTERNATE_CREDIT_CARD_LIST](CardRefID, [KEY], MIS_Field1, MIS_Field2, ValidatingCarrier
, OtherCarriers, [Card Vendor], Card, Expiration, Precedence, AlertEmail, maxTktAmt, IRP_remark)
SELECT * FROM #SecureList;
END
END
GO
I am getting an error as
Msg 120, Level 15, State 1, Procedure SP_Cde_SecureList_Upsert, Line 10 [Batch Start Line 60]
The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
This doesn't have anything to do with C# so you should remove the tag from your question. Could you provide the error message?

Why does this update query not work?

I am pretty new with SQL Server and DBMS and I have some problem with the following query that upade a single record of a table named: MaliciousCodeAlertDocument
This is my query:
UPDATE MaliciousCodeAlertDocument
SET Discovered = GetDate(),
LastUpdated = GetDate(),
SourceId = "UP",
MalCodeID = 1,
Title = "UPDATED",
Origin = "UP",
Type = "UP",
Feature = "UP",
Risk = "UP",
Severity = 1,
OverallImpact = 1,
ContagionPotential = 1,
Wild = "UP",
AlertStatusID = 1,
AttributeType = 1,
DetailLevel = 1 ,
Language = 1 ,
Summary = "UP" ,
Symptom = "UP" ,
TechnicalDescription = "UP" ,
MitigatingStrategy = "UP" ,
Disinfection = "UP" ,
URL = "UP"
WHERE Id = 11316
When I try to execute it I obtain the following error message:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UPDATED'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'UP'.
What is the problem? How can I fix it?
Tnx
Using double-quotes makes SQL Server think that the value within the quotes is a column, but it looks like you're trying to update it to a text value. If you use single quotes instead, SQL will recognize that you want to insert that value into your field, and should load it correctly.
EDIT: Sorry, didn't see that #scragar already posted this solution. Anyone know if I'm supposed to delete my answer because of this?

The multi-part identifier could not be bound using a linked server with ODBC connection to a DB2

when I run this simple query
select [B00BF4CR].[IWSE4S8].[SCTRN].[STCOMP],
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STDATE],
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STUNM],
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STQTY]
FROM [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN]
WHERE [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STCOMP]='51'
AND [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STDATE] = 20140211
AND [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STVOID] = 'N'
ORDER BY [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STTCKT]
in my sql I got the below errors
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "EPAK.B00BF4CR.IWSE4S8.SCTRN.STCOMP" could not be bound.
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "EPAK.B00BF4CR.IWSE4S8.SCTRN.STDATE" could not be bound.
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "EPAK.B00BF4CR.IWSE4S8.SCTRN.STVOID" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "B00BF4CR.IWSE4S8.SCTRN.STCOMP" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "EPAK.B00BF4CR.IWSE4S8.SCTRN.STDATE" could not be bound.
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "EPAK.B00BF4CR.IWSE4S8.SCTRN.STUNM" could not be bound.
Msg 4104, Level 16, State 1, Line 2
The multi-part identifier "EPAK.B00BF4CR.IWSE4S8.SCTRN.STQTY" could not be bound.
Msg 4104, Level 16, State 1, Line 5
The multi-part identifier "EPAK.B00BF4CR.IWSE4S8.SCTRN.STTCKT" could not be bound.
when I do double clic on the 1st one it highlight this section
FROM [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN]
WHERE [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STCOMP]='51' AND
the 2nd and the 3rd ones
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STDATE] = 20140211
AND [EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STVOID] = 'N' ORDER BY
4, 5 and 6
select [B00BF4CR].[IWSE4S8].[SCTRN].[STCOMP],
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STDATE],
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STUNM],
7
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STQTY]
8
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN].[STTCKT]
At most, you can have a four part notation. Your query is invalidating that rule for the column names. A table alias comes in handy to avoid this issue.
[LINKEDSERVER].[DATABASE].[OWNER].[OBJECT]
Here is a rewrite of the query.
-- Use a table alias
SELECT
S.[STCOMP],
S.[STDATE],
S.[STUNM],
S.[STQTY]
FROM
[EPAK].[B00BF4CR].[IWSE4S8].[SCTRN] AS S
WHERE
S.[STCOMP]='51' AND
S.[STDATE] = 20140211 AND
S.[STVOID] = 'N'
ORDER BY
S.[STTCKT]
There are some issues with updates and deletes using Distributed Query Processing (DQP) through DB2OLEDB.
See article below for warning about this as well as examples of pass thru query vs linked server.
http://support.microsoft.com/kb/222937

Change date difference with CASE calculation

I have a holding table for data, before adding to a final table in my database.
Within that table is a datediff calculation, which returns the number of days in a month.
However, this is baased on projects and what I need to do is look up the Project ID in another table, to see whether the TimeID (Year/Month) is equal to either the Start- or End-Date of the project.
When it matches either, it needs to change the number of days for that month to either Month-Start to Project-Finish (if it's the end month) or Project-Start to Month-End (if it's the start month).
I've tried to do this with the following script but I get the errors:
*** This bit is sorted, please see below ***
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'FROM'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'A'.
*** This bit is sorted, please see below ***
* EDIT *
Thanks to Anton, I've added the required END to my CASE statement, but I now get a different set of errors (probably down to my syntax):
Msg 207, Level 16, State 1, Line 23
Invalid column name 'TimeID'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'TimeID'.
Msg 207, Level 16, State 1, Line 26
Invalid column name 'ID'.
Msg 207, Level 16, State 1, Line 28
Invalid column name 'ID'.
Please help if you can.
The script is:
UPDATE FAC
SET FAC.[SignedData] =
CASE
WHEN FAC.[TIMEID] = A.[Start_TimeID] THEN
CASE
WHEN pd.[Start_Date] >= GETDATE() THEN datediff(day,pd.[Start_Date],pt.[Period_End])
WHEN pd.[Start_Date] < GETDATE() THEN datediff(day,GETDATE(),pt.[Period_End])
END
WHEN FAC.[TIMEID] = A.[End_TimeID] THEN
CASE
WHEN pd.[End_Date] >= GETDATE() THEN datediff(day,getdate(),pd.[End_Date])
WHEN pd.[End_Date] < GETDATE() THEN '0'
END
END
FROM Temp_Fac2Programme FAC
JOIN
(
SELECT
pd.[TimeID], pd.[Start_TimeID], pd.[End_TimeID], pt.[Period_Start], pt.[Period_End]
FROM ProjectDates pd
JOIN ProjectTimeID pt
on pd.[TimeID] = pt.[TimeID]
) A
ON A.[ID] = FAC.[Project]
Where A.[ID] = FAC.[Project]
GO
NB - SIGNEDDATA is the number of days in the month.
* Example Data *
Temp_Fac2Programme (FAC) contains:
ACCOUNT CATEGORY DATASRC PROFITCENTRE PROJECT RPTCURRENCY TIMEID SIGNEDDATA SOURCE
REMAIN_DAYS_FLAG ACTUAL DS_FLAGS B9059 AAA_7915_BBOY LC 20130100 34.0000000000 0
ProjectDates (pd) contains:
PROJECT_ID START_TIMEID END_TIMEID START_DATE END_DATE TOTAL_DAYS
PAG_5244_CASH 20110400 20120300 2011-04-01 2012-03-31 365
ProjectTimeID (pt) contains:
TIMEID PERIOD_START PERIOD_END DAYS_IN_PERIOD PCMONTHSTAT
20140600 2014-05-31 2014-06-27 27 F
As of the current (edited) version, it's basically what error messages say:
You use pd.[TimeID] in two places, but there is no TimeID column in ProjectDates (for now, I have no idea what you meant here).
Your subquery (aliased to A) has no ID column. The likely fix is adding pd.[Project_ID] as ID into the subquery's list of selected fields.