SQL Help - If Then Else - sql

I need the help of someone who understands SQL better than I do...
I am trying to make a single SP call that uses the IF THEN ELSE but keep getting three "the multi-part identifier could not be bound" messages.
Pages can be sponsored by a Business or a Group.
In the advertEntityType field I have a "B" or a "G" for business or group.
In the advertEntityID field I have the businessID or groupID.
SELECT
Advertisements.advertEntityType
,Advertisements.advertEntityID
FROM Advertisements
WHERE Advertisements.advertPageName = #pageName
IF (Advertisements.advertEntityType = 'G')
SELECT
Group.groupID
,Group.groupName
,Group.groupPhone
,Group.groupWebsite
,Group.groupLogoName
FROM Group
WHERE Group.groupID = Advertisements.advertEntityID
ELSE
SELECT
Business.businessID
,Business.businessName
,Business.businessWorkPhone
,Business.businessWebsite
,Business.businessLogoName
FROM Business
WHERE Business.businessID = Advertisements.advertEntityID
When I try to execute I get these messages which I cannot seem to get my head around...
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 36
The multi-part identifier "Advertisements.advertEntityType" could not be bound.
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 46
The multi-part identifier "Advertisements.advertEntityID" could not be bound.
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 58
The multi-part identifier "Advertisements.advertEntityID" could not be bound.

Advertisements.advertEntityType is a table name and column name. You need to assign these values to local variables if you want to use them in if statements. If I understand correctly you want to evaluate based on the condition in the first select statement. In this case you should use such query:
DECLARE #entityType int --assuming the type of Advertisements.advertEntityType is int
DECLARE #advertEntityID int --assuming the type of Advertisements.advertEntityID is int
SELECT
#entityType = Advertisements.advertEntityType,
#advertEntityID = Advertisements.advertEntityID
FROM Advertisements
WHERE Advertisements.advertPageName = #pageName
IF (#entityType = 'G')
SELECT
Group.groupID
,Group.groupName
,Group.groupPhone
,Group.groupWebsite
,Group.groupLogoName
FROM Group
WHERE Group.groupID = #advertEntityID
ELSE
SELECT
Business.businessID
,Business.businessName
,Business.businessWorkPhone
,Business.businessWebsite
,Business.businessLogoName
FROM Business
WHERE Business.businessID = #advertEntityID

Related

Could not find server 'STLEDGSQL01' in sysservers. Execute sp_addlinkedserver to add the server to sysservers?

The following SQL script is what I'm trying to execute using SQL Server Management Studio to get my end result and one of the stackoverflow member answered this script as per my needs, so I'm assuming the script is correct. But the issue is when I execute the script it gives me the following error.
Script
SELECT
ID,
RTRIM(trx_date), RTRIM(work_order), RTRIM(department),
RTRIM(work_center), RTRIM(operation_no), RTRIM(operator),
RTRIM(total_labor_hours), RTRIM(feet_produced),
RTRIM(item_no), RTRIM(lot_no), RTRIM(default_bin), RTRIM(posted)
FROM
NOR_LABOR
WHERE
ID > (SELECT MAX(SOURCE_ID)
FROM [STLEDGSQL01].[MES_DEV].[dbo].[ALL_LABOR_DETAILS]
WHERE work_center LIKE ('%NOR%'))
ORDER BY
ID
Error
Msg 7202, Level 11, State 2, Line 1
Could not find server 'STLEDGSQL01' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.
I have tried to execute that statement in many different ways since my colleagues advised me adding the 'SERVER' and 'DATABASE' might be little extraneous but still keep getting errors as below
Keeping all the source and destination server/database/table name.
SELECT * FROM [Normac Data].[dbo].[NOR_LABOR] WHERE ID > (SELECT MAX(SOURCE_ID) FROM [STLEDGSQL01].[MES_DEV].[dbo].[ALL_LABOR_DETAILS] WHERE work_center LIKE '%NOR%') ORDER BY ID
Error : Msg 7202, Level 11, State 2, Line 1
Could not find server 'STLEDGSQL01' in sysservers. Execute sp_addlinkedserver to add the server to sysservers.
Removing the destination 'server' only and keeping everything else same.
SELECT * FROM [Normac Data].[dbo].[NOR_LABOR] WHERE ID > (SELECT MAX(SOURCE_ID) FROM [MES_DEV].[dbo].[ALL_LABOR_DETAILS] WHERE work_center LIKE '%NOR%') ORDER BY ID
Error : Msg 208, Level 16, State 1, Line 2
Invalid object name 'MES_DEV.dbo.ALL_LABOR_DETAILS'.
Removing the destination 'server' and 'database' only and keeping everything else same.
SELECT * FROM [Normac Data].[dbo].[NOR_LABOR] WHERE ID > (SELECT MAX(SOURCE_ID) FROM [dbo].[ALL_LABOR_DETAILS] WHERE work_center LIKE '%NOR%') ORDER BY ID
Error : Msg 208, Level 16, State 1, Line 3
Invalid object name 'dbo.ALL_LABOR_DETAILS'.
Removing the destination 'server' and 'database' and 'dbo' only and keeping everything else same.
SELECT * FROM [Normac Data].[dbo].[NOR_LABOR] WHERE ID > (SELECT MAX(SOURCE_ID) FROM ALL_LABOR_DETAILS WHERE work_center LIKE '%NOR%') ORDER BY ID
Error : Msg 208, Level 16, State 1, Line 4
Invalid object name 'ALL_LABOR_DETAILS'.
Removing the source server only and keeping everything else same.
SELECT * FROM [dbo].[NOR_LABOR] WHERE ID > (SELECT MAX(SOURCE_ID) FROM ALL_LABOR_DETAILS WHERE work_center LIKE '%NOR%') ORDER BY ID
Error : Msg 208, Level 16, State 1, Line 8
Invalid object name 'ALL_LABOR_DETAILS'.
Removing the source server and database only and keeping everything else same.
SELECT * FROM NOR_LABOR WHERE ID > (SELECT MAX(SOURCE_ID) FROM ALL_LABOR_DETAILS WHERE work_center LIKE '%NOR%') ORDER BY ID
Error : Msg 208, Level 16, State 1, Line 9
Invalid object name 'ALL_LABOR_DETAILS'.
I tried everything that I know but still not working, what else can I do to make this query work? Thank you all!

The multi-part identifier could not be bound error with update statement and where exists clause

My query is as shown below . I am getting error as
Msg 4104, Level 16, State 1, Procedure USP_Group11HtmlFileDetails,
Line 71
The multi-part identifier "t_FI.Fullimage" could not be bound.
Msg 4104, Level 16, State 1, Procedure USP_Group11HtmlFileDetails,
Line 71
The multi-part identifier "t_FI.Caption" could not be bound.
#tblGroup11HtmlFileImages is table variable which is of type exactly similar to ta
Query
UPDATE FI
SET FI.Fullimage = t_FI.Fullimage,
FI.Caption = t_FI.Caption
FROM tblGroup11HtmlFileImages FI
WHERE EXISTS (SELECT *
FROM #tblGroup11HtmlFileImages t_FI
WHERE t_FI.[FileName] = FI.[FileName]
AND t_FI.Thumbnail = FI.Thumbnail)
The exists operator only checks for existence of a value, no value is actually retrieved from the Exists operator.
You need to join these two tables something like....
UPDATE FI
SET FI.Fullimage = t_FI.Fullimage
, FI.Caption = t_FI.Caption
FROM tblGroup11HtmlFileImages FI
INNER JOIN #tblGroup11HtmlFileImages t_FI
ON t_FI.[FileName] = FI.[FileName]
AND t_FI.Thumbnail = FI.Thumbnail

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.

Dynamic UPDATE statement

I want to create a dynamic update query where I need to set a certain value in a column. But the column name needs to be SELECTed from another table.
I have already the following query:
UPDATE core.TableRes
SET (
SELECT Code FROM core.TableFields
INNER JOIN core.TableXTableFields ON TableXTableFields.FieldID = TableFields.FieldID
INNER JOIN core.TableResRefLinks ON TableResRefLinks.ExtraFieldID = TableXTableFields.ExtraFieldID
WHERE TableResRefLinks.TableResRefLinksID = RefLinks.TableResRefLinksID)
= (
SELECT Value FROM core.TableResRefLinks WHERE TableResRefLinksID = RefLinks.TableResRefLinksID)
FROM core.TableRes
INNER JOIN core.TableResRefLinks RefLinks ON RefLinks.ResourceID = TableRes.ResourceID
INNER JOIN core.TableXTableFields ON TableXTableFields.ExtraFieldID = RefLinks.ExtraFieldID
INNER JOIN core.TableFields ON TableFields.FieldID = TableXTableFields.FieldID
WHERE (EndDate IS NULL OR EndDate > GETDATE()) AND
(
SELECT Code FROM core.TableFields
INNER JOIN core.TableXTableFields ON TableXTableFields.FieldID = TableFields.FieldID
INNER JOIN core.TableResRefLinks ON TableResRefLinks.ExtraFieldID = TableXTableFields.ExtraFieldID
WHERE TableResRefLinks.TableResRefLinksID = RefLinks.TableResRefLinksID)
<>
(
SELECT Value FROM core.TableResRefLinks
WHERE TableResRefLinksID = RefLinks.TableResRefLinksID)
It gives me the following errors:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'FROM'.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near '<'.
Is there a way to solve this? If I change the complete UPDATE and SET statements and replace them with a SELECT *, I get results.
EDIT
Here are the datatypes
TableFields.Code => nvarchar(100)
TableResRefLinks.Value => sql_variant
And the datatypes of the columns that have as column name TableFields.Code are set as sql_variant
You can't solve this using plain SQL. You would need some kind of scripting to build your statement. For example postgresql has a scripting language called "pgpsql" which allows building dynamic SQL statements. But this clearly depends on the underlying RDBMS.
By the way: this works with SELECT as you are doing simple sub-select.