Jump to content

Recommended Posts

1. Just post code normally. With the code highlighting. Most people don't bother looking at attachments.

2. Don't post database credentials. Stop what you're doing and go change your password. Right now.

 

$query ="
//set NOCOUNT ON /*optional*/
SELECT 
cast(h1.Weeknum AS varchar)
+'|'
+cast(h1.Year AS varchar)
+'|'
+QUOTENAME(CASE 
		WHEN Cty.CountryName = 'Singapore' THEN 'SG' 
		WHEN Cty.CountryName = 'Hong Kong' THEN 'HK' 
		WHEN Cty.CountryName = 'Japan' THEN 'JP'
		WHEN Cty.CountryName = 'China' THEN 'CN'
		WHEN Cty.CountryName = 'Indonesia' THEN 'ID'
		WHEN Cty.CountryName = 'Taiwan' THEN 'TW'
		WHEN Cty.CountryName = 'Thailand' THEN 'TH'
		WHEN Cty.CountryName = 'Philippines' THEN 'PH'
		WHEN Cty.CountryName = 'Macau' THEN 'MO'
		WHEN Cty.CountryName = 'Malaysia' THEN 'MY'
	ELSE '' END , '""') 
+'|' + QUOTENAME(Loc.LocationName,'"') 
 
+'|'
+QUOTENAME(CASE 
		WHEN Cust.CustomerName = 'McDonalds'THEN'MCD' 
		WHEN Cust.CustomerName  = 'Subway' THEN 'SWY' 
		WHEN Cust.CustomerName = 'Oishi' THEN 'OIS'
		WHEN Cust.CustomerName = 'Shell' THEN 'SHE'
		WHEN Cust.CustomerName = 'FSB' THEN 'FSB'
		WHEN Cust.CustomerName = 'Ikea' THEN 'IKE'
		WHEN Cust.CustomerName = 'Yellow Cab' THEN 'YEL'
		WHEN Cust.CustomerName = 'PUB' THEN 'PUB'
	ELSE '' END, '""') 
+'|'+cast(round(k1.[Value], 0) as varchar) 
+'|'+'0' -- Tonnage Delivered, assume 0 for now
+'|'+cast(k2.[VALUE] as varchar) 
+'|'+cast(k3.[VALUE] as varchar) 
+'|'+cast(k4.[VALUE] as varchar)
+'|'+'0' -- Deliveries with error, assume 0 for now
+'|'+cast(k5.[VALUE] as varchar)
+'|'+cast((round(k9.[VALUE]+k1.[VALUE],0)) as varchar)
+'|'+cast(round(k7.[VALUE],0) as varchar)
+'|'+'0' -- Tonnage Handled, assume 0 for now
+'|'+cast(round(k8.[VALUE],0) as varchar)
+'|'+'20151130'
+'|'+'20151206'
FROM [KeyPerformance].[dbo].[KpiDetails] as k1,
[KeyPerformance].[dbo].[KpiDetails] as k2,
  [KeyPerformance].[dbo].[KpiDetails] as k3,
  [KeyPerformance].[dbo].[KpiDetails] as k4,
   [KeyPerformance].[dbo].[KpiDetails] as k5,
  [KeyPerformance].[dbo].[KpiDetails] as k6,
  [KeyPerformance].[dbo].[KpiDetails] as k7,
  [KeyPerformance].[dbo].[KpiDetails] as k8,
   [KeyPerformance].[dbo].[KpiDetails] as k9,
  [KeyPerformance].[dbo].[KpiHeader] as h1,
  [KeyPerformance].[dbo].[Customer] as Cust,
  [KeyPerformance].[dbo].[Location] as Loc,
  [KeyPerformance].[dbo].[Country] as Cty,
    [KeyPerformance].[dbo].[CalendarWeekOfYear] as Cal
  where k1.[KpiHeaderID] = k2.[KpiHeaderID]
   AND k2.[KpiHeaderID] = k3.[KpiHeaderID]
   AND k3.[KpiHeaderID] = k4.[KpiHeaderID]
   AND k4.[KpiHeaderID] = k5.[KpiHeaderID]
   AND k5.[KpiHeaderID] = k6.[KpiHeaderID]
   AND k6.[KpiHeaderID] = k7.[KpiHeaderID]
    AND k7.[KpiHeaderID] = k8.[KpiHeaderID]
	  AND k8.[KpiHeaderID] = k9.[KpiHeaderID]
   AND k1.[KpiHeaderID] = h1.[KpiHeaderID]
   AND h1.[CustomerID] = Cust.[CustomerID]
      AND h1.[LocationID] = Loc.[LocationID]
   AND Loc.[CountryID] = Cty.[CountryID]
   AND h1.[WeekNum] = Cal.WeekOfYear
    AND h1.[Year] = Cal.Year
-- AND k1.[KpiHeaderID] = 122 
  AND k1.[KpiItemsID] = 2
  AND k2.[KpiItemsID] = 1
  AND k3.[KpiItemsID] = 9
  AND k4.[KpiItemsID] = 10
  AND k5.[KpiItemsID] = 31
  AND k6.[KpiItemsID] = 2
  AND k7.[KpiItemsID] = 18
  AND k8.[KpiItemsID] = 11
    AND k9.[KpiItemsID] = 3
AND h1.Year = '2015' and Cal.WeekofYear = 48
--AND Cal.MonthID = 11;"
You have unescaped "s within that string.

*Edit: Confused answer with the makePass function.

What?

$query = <<<SQL
//set NOCOUNT ON /*optional*/
SELECT 
cast(h1.Weeknum AS varchar)
+'|'
+cast(h1.Year AS varchar)
+'|'
+QUOTENAME(CASE 
		WHEN Cty.CountryName = 'Singapore' THEN 'SG' 
		WHEN Cty.CountryName = 'Hong Kong' THEN 'HK' 
		WHEN Cty.CountryName = 'Japan' THEN 'JP'
		WHEN Cty.CountryName = 'China' THEN 'CN'
		WHEN Cty.CountryName = 'Indonesia' THEN 'ID'
		WHEN Cty.CountryName = 'Taiwan' THEN 'TW'
		WHEN Cty.CountryName = 'Thailand' THEN 'TH'
		WHEN Cty.CountryName = 'Philippines' THEN 'PH'
		WHEN Cty.CountryName = 'Macau' THEN 'MO'
		WHEN Cty.CountryName = 'Malaysia' THEN 'MY'
	ELSE '' END , '""') 
+'|' + QUOTENAME(Loc.LocationName,'"') 
 
+'|'
+QUOTENAME(CASE 
		WHEN Cust.CustomerName = 'McDonalds'THEN'MCD' 
		WHEN Cust.CustomerName  = 'Subway' THEN 'SWY' 
		WHEN Cust.CustomerName = 'Oishi' THEN 'OIS'
		WHEN Cust.CustomerName = 'Shell' THEN 'SHE'
		WHEN Cust.CustomerName = 'FSB' THEN 'FSB'
		WHEN Cust.CustomerName = 'Ikea' THEN 'IKE'
		WHEN Cust.CustomerName = 'Yellow Cab' THEN 'YEL'
		WHEN Cust.CustomerName = 'PUB' THEN 'PUB'
	ELSE '' END, '""') 
+'|'+cast(round(k1.[Value], 0) as varchar) 
+'|'+'0' -- Tonnage Delivered, assume 0 for now
+'|'+cast(k2.[VALUE] as varchar) 
+'|'+cast(k3.[VALUE] as varchar) 
+'|'+cast(k4.[VALUE] as varchar)
+'|'+'0' -- Deliveries with error, assume 0 for now
+'|'+cast(k5.[VALUE] as varchar)
+'|'+cast((round(k9.[VALUE]+k1.[VALUE],0)) as varchar)
+'|'+cast(round(k7.[VALUE],0) as varchar)
+'|'+'0' -- Tonnage Handled, assume 0 for now
+'|'+cast(round(k8.[VALUE],0) as varchar)
+'|'+'20151130'
+'|'+'20151206'
FROM [KeyPerformance].[dbo].[KpiDetails] as k1,
[KeyPerformance].[dbo].[KpiDetails] as k2,
  [KeyPerformance].[dbo].[KpiDetails] as k3,
  [KeyPerformance].[dbo].[KpiDetails] as k4,
   [KeyPerformance].[dbo].[KpiDetails] as k5,
  [KeyPerformance].[dbo].[KpiDetails] as k6,
  [KeyPerformance].[dbo].[KpiDetails] as k7,
  [KeyPerformance].[dbo].[KpiDetails] as k8,
   [KeyPerformance].[dbo].[KpiDetails] as k9,
  [KeyPerformance].[dbo].[KpiHeader] as h1,
  [KeyPerformance].[dbo].[Customer] as Cust,
  [KeyPerformance].[dbo].[Location] as Loc,
  [KeyPerformance].[dbo].[Country] as Cty,
    [KeyPerformance].[dbo].[CalendarWeekOfYear] as Cal
  where k1.[KpiHeaderID] = k2.[KpiHeaderID]
   AND k2.[KpiHeaderID] = k3.[KpiHeaderID]
   AND k3.[KpiHeaderID] = k4.[KpiHeaderID]
   AND k4.[KpiHeaderID] = k5.[KpiHeaderID]
   AND k5.[KpiHeaderID] = k6.[KpiHeaderID]
   AND k6.[KpiHeaderID] = k7.[KpiHeaderID]
    AND k7.[KpiHeaderID] = k8.[KpiHeaderID]
	  AND k8.[KpiHeaderID] = k9.[KpiHeaderID]
   AND k1.[KpiHeaderID] = h1.[KpiHeaderID]
   AND h1.[CustomerID] = Cust.[CustomerID]
      AND h1.[LocationID] = Loc.[LocationID]
   AND Loc.[CountryID] = Cty.[CountryID]
   AND h1.[WeekNum] = Cal.WeekOfYear
    AND h1.[Year] = Cal.Year
-- AND k1.[KpiHeaderID] = 122 
  AND k1.[KpiItemsID] = 2
  AND k2.[KpiItemsID] = 1
  AND k3.[KpiItemsID] = 9
  AND k4.[KpiItemsID] = 10
  AND k5.[KpiItemsID] = 31
  AND k6.[KpiItemsID] = 2
  AND k7.[KpiItemsID] = 18
  AND k8.[KpiItemsID] = 11
    AND k9.[KpiItemsID] = 3
AND h1.Year = '2015' and Cal.WeekofYear = 48
--AND Cal.MonthID = 11;
SQL;
(huh. okay. pretend there's appropriate highlighting)

 

Doing that would have completely bypassed the quotes problem.

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.