Unexpected output when executed - sql

I have a problem in SQL Server, when I try to select two columns.
Here is the code I have written:
declare #d varchar(max)
set #d = ''
begin
select #d = #d + convert(nvarchar(MAX), R.Data, 121) + ('<table></td><td>' + convert(nvarchar(MAX), C.No) + '</td><td>' + C.Nome + '</td><td>' + convert(nvarchar(MAX), V.IdVendedor) + '</td><td>' + convert(nvarchar(MAX), V.Vendnm) + '</td></tr></table>')
from dbo.Reclamacoes R
Inner Join dbo.PHC_CLIENTES_SAMSYS C On R.IdCliente = C.Id
Inner Join dbo.PHC_VENDEDORES_SAMSYS V on R.IdVendedor = V.IdVendedor
select #d As Data, #d As HTML_COLUMN
end
It shows like this:
2019-02-07 00:00:00.0000000 " <table></td><td>2762.00</td><td>REGINA & MIGUEL, LDA. ( REMI )</td><td>78.00</td><td>AndreiaVeloso</td></tr></table> "
As you see, data is mixed with what is supposed to appear in the other column (I put in "" what it's supposed to appear in the other column).
And when I execute the column know as "Data" is mixed with " HTML_COLUMN.
And I'm trying to get two columns just with one variable.
Can someone tell me if is possible in my case, to have a query showing two columns when having just one variable? Thanks in advance.

You can make #d a Table Variable and put two columns in it.
Example from MSDN:
DECLARE #MyTableVar table(
EmpID int NOT NULL,
OldVacationHours int,
NewVacationHours int,
ModifiedDate datetime);

Try this..
DECLARE #HTML NVARCHAR(MAX) = '';
SET #HTML
= N'<table border="1" >'
+ N'<tr><th>Data</th><th>Nome</th><th>IdVendedor</th><th>Vendnm</th>'
+ CAST((
SELECT
td = R.Data
, ''
, td = C.Nome
, ''
, td = V.IdVendedor
, ''
, td = V.Vendnm
FROM dbo.Reclamacoes R
INNER JOIN dbo.PHC_CLIENTES_SAMSYS C ON R.IdCliente = C.Id
INNER JOIN dbo.PHC_VENDEDORES_SAMSYS V ON R.IdVendedor = V.IdVendedor
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX)) + N'</table>';
SELECT #HTML;

Related

COALESCE Doubles results values

in function
SELECT #ProcedureName = COALESCE(#ProcedureName + ',', '') + ProcedureName
FROM V_Procedures P
inner join MedicalRecords MR on P.Medical_Record=MR.Medical_Record
where ProcedureName IS NOT NULL and MR.Admission_No  = #Admission_No
and Field = 18
getting many doubles seperated by comma, is it possible to avoid it?
For example results values appears more then once as
CONCHOTOMY,FESS-FUNCTIONAL ENDOSCOPIC (NASAL) SINUS SURGERY,CONCHOTOMY,FESS-FUNCTIONAL ENDOSCOPIC (NASAL) SINUS SURGERY,SUBMUCOUS RESECTION OF NASAL SEPTUM
You seems want distinct :
SELECT DISTINCT #ProcedureName = COALESCE(#ProcedureName + ',', '') + ProcedureName
FROM V_Procedures P INNER JOIN
MedicalRecords MR
ON P.Medical_Record=MR.Medical_Record
WHERE ProcedureName IS NOT NULL AND
MR.Admission_No = #Admission_No AND
Field = 18;
Instead COALESCE, try with CONCAT. And also GROUP BY.
DECLARE #ProcedureName VARCHAR(max);
SELECT #ProcedureName = CONCAT(#ProcedureName + ',', ProcedureName)
FROM V_Procedures P
JOIN MedicalRecords MR ON P.Medical_Record = MR.Medical_Record
WHERE ProcedureName IS NOT NULL AND MR.Admission_No = #Admission_No
AND Field = 18
GROUP BY ProcedureName
The trick is that CONCAT won't return NULL when a NULL is added to it. But + does. So because #ProcedureName is NULL at first, the result won't start with a comma.

Invalid length parameter passed to the LEFT or SUBSTRING function in my query. It runs perfect id DTS 2008 but gives this error in SSIS 2014

DECLARE #ColumnTotal INT,
#HTProcOfficeColumnNames NVARCHAR(4000),
#TBLColumnNames NVARCHAR(4000),
#query NVARCHAR(4000),
#subqueryInsert NVARCHAR(4000),
#subqueryValues NVARCHAR(4000)
SELECT #ColumnTotal = Count(*)
FROM (SELECT htprocoffice
FROM table
WHERE COLUMN NOT LIKE '% - CO'
GROUP BY COLUMN) T
SELECT #HTProcOfficeColumnNames = COALESCE(#HTProcOfficeColumnNames
+ ',[' + htprocoffice + ']', '[' + htprocoffice + ']')
FROM (SELECT htprocoffice
FROM table
WHERE COLUMN NOT LIKE '% - CO'
GROUP BY COLUMN) T
ORDER BY COLUMN
SELECT #TBLColumnNames = COALESCE(#TBLColumnNames + ',' + NAME, NAME)
FROM table
WHERE id IN (SELECT id
FROM table
WHERE type = 'U'
AND NAME = 'TableName')
AND NAME LIKE 'HTOffice%'
ORDER BY NAME
Is there any error in the below code?
set #subqueryInsert = substring(#TBLColumnNames,1,#ColumnTotal*11-1) --The error occurs here but not sure though.
set #subqueryInsert = 'INSERT INTO Table
(RunDate,OrderBy,Period,IConOffice,IConOfficeType,'+ #subqueryInsert+')'
set #subqueryValues =
replace(replace(#HTProcOfficeColumnNames,'[',''''), ']','''')

Operand type clash: varchar is incompatible with User-Defined Table Type

I created a User-Defined Table Type:
CREATE TYPE dbo.ListTableType AS TABLE(
ITEM varchar(500) NULL
)
I leverage it in a function:
CREATE FUNCTION dbo.fn_list_to_string
(
#LIST dbo.ListTableType READONLY
)
RETURNS varchar(max)
AS
BEGIN
DECLARE #RESULT varchar(max)
SET #RESULT = ''
DECLARE #NL AS CHAR(2) = CHAR(13) + CHAR(10)
SELECT #RESULT = #RESULT + ITEM + #NL FROM #LIST
SET #RESULT = SUBSTRING(#RESULT, 1, LEN(#RESULT) - 1)
RETURN #RESULT
END
Finally, I try to use this function in a simple select:
SELECT
P.PROGRAM_ID,
PROGRAM_NAME,
PROGRAM_DESC,
P.STATUS_ID,
STATUS_DESC,
P.CONTACT_SID,
I.FIRST_NAME + ' ' + I.LAST_NAME as CONTACT_NAME,
P.CLARITY_ID,
dbo.fn_list_to_string(
( SELECT CONVERT(varchar,CLARITY_ID) as ITEM
FROM dbo.MUSEUM_PROGRAM_PROJECTS as A
JOIN dbo.MUSEUM_PROJECTS as B on B.PROJECT_ID = A.PROJECT_ID
WHERE PROGRAM_ID = P.PROGRAM_ID )
) as PROJECT_CLARITY_IDS
FROM dbo.MUSEUM_PROGRAMS as P
LEFT JOIN dbo.MUSEUM_PROGRAM_STATUS_TYPES as S on S.STATUS_ID = P.STATUS_ID
LEFT JOIN dbo.v_IDVAULT_ENRICHED_CURRENT_EMPLOYEES as I on I.[SID] = P.CONTACT_SID
But I get this error:
Operand type clash: varchar is incompatible with ListTableType
Any idea why? Also if there's another [more elegant] way to achieve what I'm trying to do I'm open to suggestions as well! Thanks in advance!
Here is a simple demonstration of the FOR XML PATH technique which does all of this with a very simple subquery and no table types or extremely inefficient multi-statement table-valued functions etc.
USE tempdb;
GO
CREATE TABLE dbo.P(Program_ID INT);
CREATE TABLE dbo.M(Clarity_ID INT, Program_ID INT);
INSERT dbo.P VALUES(1),(2),(3),(4);
INSERT dbo.M VALUES(1,1),(1,2),(2,3),(3,2),(1,4),(4,1);
SELECT
P.PROGRAM_ID,
PROJECT_CLARITY_IDS = STUFF((
SELECT CHAR(13)+CHAR(10)+CONVERT(VARCHAR(12),Clarity_ID)
FROM dbo.M WHERE Program_ID = p.Program_ID
FOR XML PATH(''), TYPE).value('.[1]','nvarchar(max)'),1,2,'')
FROM dbo.P AS p;
SQLfiddle demo
The output doesn't look right in SQLfiddle or in results to grid in Management Studio, because they strip out carriage returns/line feeds for display purposes, but you can replace CHAR(13)+CHAR(10) with two commas or semi-colons or something to verify that there are two characters there.
Using STUFF..FOR XML PATH construct for concatanation in combination with CTE will get the results you'd like. Something like this:
WITH CTE_PROJECT_CLARITIES AS
(
SELECT DISTINCT PROGRAM_ID
, STUFF((
SELECT CHAR(13) + CHAR(10) + CONVERT(varchar(11),CLARITY_ID)
FROM dbo.MUSEUM_PROGRAM_PROJECTS as A
JOIN dbo.MUSEUM_PROJECTS as B on B.PROJECT_ID = A.PROJECT_ID
WHERE A.PROGRAM_ID = X.PROGRAM_ID
FOR XML PATH ('')),1,2,'') AS PROJECT_CLARITY_IDS
FROM MUSEUM_PROGRAM_PROJECTS X
)
SELECT
P.PROGRAM_ID,
PROGRAM_NAME,
PROGRAM_DESC,
P.STATUS_ID,
STATUS_DESC,
P.CONTACT_SID,
I.FIRST_NAME + ' ' + I.LAST_NAME as CONTACT_NAME,
P.CLARITY_ID,
X.PROJECT_CLARITY_IDS
FROM dbo.MUSEUM_PROGRAMS as P
LEFT JOIN dbo.MUSEUM_PROGRAM_STATUS_TYPES as S on S.STATUS_ID = P.STATUS_ID
LEFT JOIN dbo.v_IDVAULT_ENRICHED_CURRENT_EMPLOYEES as I on I.[SID] = P.CONTACT_SID
LEFT JOIN CTE_PROJECT_CLARITIES X ON X.PROGRAM_ID = p.PROGRAM_ID
SQLFiddle DEMO (not sure if I got the columns right, but you'll get the idea)

n to n relationship how to obtain a result in one row

so I have an employee that can work in many companies so I have an n to n relationship, how can I obtain the companies that one employee works in, in just one row with sql?
example
table - employee
Employeeid employeename
1 mike
table company
companyId CompanyName
1 cocacola
2 nokia
3 intel
table employeeCompany
id employeeid companyid
1 1 1
2 1 2
3 1 3
I thought with this but can´t
select Employeeid , companyid
from employeeCompany
where employeeid = 1
group by Employeeid , companyid
Easiest way to do it in Sql Server is by use of FOR XML PATH. The cryptic part .value('text()[1]','nvarchar(max)') handles special xml characters.
select employee.*, companies.*
from Employee
OUTER APPLY
(
select stuff ((SELECT ', ' + Company.CompanyName
FROM EmployeeCompany
INNER JOIN Company
ON EmployeeCompany.CompanyId = Company.CompanyID
WHERE EmployeeCompany.employeeid = Employee.EmployeeID
ORDER BY Company.CompanyName
FOR XML PATH(''),TYPE).value('text()[1]','nvarchar(max)')
, 1, 2, '') Companies
) companies
See demo at Sql Fiddle.
It sounds like you want something that is similar to mySQL's Group_Concat in SQL Server?
If you are looking for a way to do this so that each companyid is in a separate column, then that would only be possible with some difficulty using dynamic SQL. At which time it might be easier to just return this to an application and let it handle what it needs within its own logic?
BTW, the dynamic SQL logic would go something like this in case you were wondering...notice how nasty it is...thus why I would suggest against it.
select #highestCount = max(count(*))
from employeeCompany
group by Employeeid
declare createtemptable varchar(max), #filltableselect varchar(max), #filltablejoin varchar(max)
declare #currentCount int
set #currentCount = 0
set #createtemptable = 'CREATE TABLE #Temp (EmployeeID INT'
set #filltableselect = 'INSERT INTO #Temp SELECT EmployeeCompany0.EmployeeID, EmployeeCompany0.CompanyID'
set #filltablejoin = 'FROM EmployeeCompany AS EmployeeCompany0'
while(#currentCount < #highestCount)
begin
set #createtemptable = #createtemptable + ', CompanyID'
+ CAST(#currentCount AS VARCHAR(2)) + ' INT'
if(#currentCount > 0)
begin
set #filltableselect = #filltableselect + ', EmployeeCompany'
+ CAST(#currentCount AS VARCHAR(2)) + '.CompanyId'
set #filltablejoin = #filltablejoin
+ 'LEFT JOIN EmployeeCompany AS EmployeeCompany'
+ CAST(#currentCount AS VARCHAR(2))
+ ' ON EmployeeCompany0.EmployeeID = EmployeeCompany'
+ CAST(#currentCount AS VARCHAR(2)) + '.EmployeeID'
end
set #currentCount = #currentCount + 1
end
set #createtemptable = #createtemptable + ')'
--This next line can be whatever you need it to be
set #filltablejoin = #filltablejoin + 'WHERE employeeCompany0.EmployeeID = 1'
exec #createtemptable
exec #filltableselect + #filltablejoin

STORED PROCEDURE working in my local test machine cannot be created in production environment

I have an SQL CREATE PROCEDURE statement that runs perfectly in my local SQL Server, but cannot be recreated in production environment. The error message I get in production is Msg 102, Level 15, State 1, Incorrect syntax near '='.
It is a pretty big query and I don't want to annoy StackOverflow users, but I simply can't find a solution. If only you could point me out what settings I could check in the production server in order to enable running the code... I must be using some kind of syntax or something that is conflicting with some setting in production. This PROCEDURE was already registered in production before, but when I ran a DROP - CREATE PROCEDURE today, the server was able to drop the procedure, but not to recreate it.
I will paste the code below. Thank you!
===============
USE [Enorway]
GO
/****** Object: StoredProcedure [dbo].[Spel_CM_ChartsUsersTotals] Script Date: 03/17/2010 11:59:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROC [dbo].[Spel_CM_ChartsUsersTotals]
#IdGroup int,
#IdAssessment int,
#UserId int
AS
SET NOCOUNT ON
DECLARE #RequiredColor varchar(6)
SET #RequiredColor = '3333cc'
DECLARE #ManagersColor varchar(6)
SET #ManagersColor = '993300'
DECLARE #GroupColor varchar(6)
SET #GroupColor = 'ff0000'
DECLARE #SelfColor varchar(6)
SET #SelfColor = '336600'
DECLARE #TeamColor varchar(6)
SET #TeamColor = '993399'
DECLARE #intMyCounter tinyint
DECLARE #intManagersPosition tinyint
DECLARE #intGroupPosition tinyint
DECLARE #intSelfPosition tinyint
DECLARE #intTeamPosition tinyint
SET #intMyCounter = 1
-- Table that will hold the subtotals...
DECLARE #tblTotalsSource table
(
IdCompetency int,
CompetencyName nvarchar(200),
FunctionRequiredLevel float,
ManagersAverageAssessment float,
SelfAssessment float,
GroupAverageAssessment float,
TeamAverageAssessment float
)
INSERT INTO #tblTotalsSource
(
IdCompetency,
CompetencyName,
FunctionRequiredLevel,
ManagersAverageAssessment,
SelfAssessment,
GroupAverageAssessment,
TeamAverageAssessment
)
SELECT
e.[IdCompetency],
dbo.replaceAccentChar(e.[Name]) AS CompetencyName,
(i.[LevelNumber]) AS FunctionRequiredLevel,
(
SELECT
ROUND(avg(CAST(ac.[LevelNumber] AS float)),0)
FROM
Spel_CM_AssessmentsData aa
INNER JOIN Spel_CM_CompetenciesLevels ab ON aa.[IdCompetencyLevel] = ab.[IdCompetencyLevel]
INNER JOIN Spel_CM_Levels ac ON ab.[IdLevel] = ac.[IdLevel]
INNER JOIN Spel_CM_AssessmentsEvents ad ON aa.[IdAssessmentEvent] = ad.[IdAssessmentEvent]
WHERE
aa.[EvaluatedUserId] = #UserId AND
aa.[AssessmentType] = 't' AND
aa.[IdGroup] = #IdGroup AND
ab.[IdCompetency] = e.[IdCompetency] AND
ad.[IdAssessment] = #IdAssessment
) AS ManagersAverageAssessment,
(
SELECT
bc.[LevelNumber]
FROM
Spel_CM_AssessmentsData ba
INNER JOIN Spel_CM_CompetenciesLevels bb ON ba.[IdCompetencyLevel] = bb.[IdCompetencyLevel]
INNER JOIN Spel_CM_Levels bc ON bb.[IdLevel] = bc.[IdLevel]
INNER JOIN Spel_CM_AssessmentsEvents bd ON ba.[IdAssessmentEvent] = bd.[IdAssessmentEvent]
WHERE
ba.[EvaluatedUserId] = #UserId AND
ba.[AssessmentType] = 's' AND
ba.[IdGroup] = #IdGroup AND
bb.[IdCompetency] = e.[IdCompetency] AND
bd.[IdAssessment] = #IdAssessment
) AS SelfAssessment,
(
SELECT
ROUND(avg(CAST(cc.[LevelNumber] AS float)),0)
FROM
Spel_CM_AssessmentsData ca
INNER JOIN Spel_CM_CompetenciesLevels cb ON ca.[IdCompetencyLevel] = cb.[IdCompetencyLevel]
INNER JOIN Spel_CM_Levels cc ON cb.[IdLevel] = cc.[IdLevel]
INNER JOIN Spel_CM_AssessmentsEvents cd ON ca.[IdAssessmentEvent] = cd.[IdAssessmentEvent]
WHERE
ca.[EvaluatedUserId] = #UserId AND
ca.[AssessmentType] = 'g' AND
ca.[IdGroup] = #IdGroup AND
cb.[IdCompetency] = e.[IdCompetency] AND
cd.[IdAssessment] = #IdAssessment
) AS GroupAverageAssessment,
(
SELECT
ROUND(avg(CAST(dc.[LevelNumber] AS float)),0)
FROM
Spel_CM_AssessmentsData da
INNER JOIN Spel_CM_CompetenciesLevels db ON da.[IdCompetencyLevel] = db.[IdCompetencyLevel]
INNER JOIN Spel_CM_Levels dc ON db.[IdLevel] = dc.[IdLevel]
INNER JOIN Spel_CM_AssessmentsEvents dd ON da.[IdAssessmentEvent] = dd.[IdAssessmentEvent]
WHERE
da.[EvaluatedUserId] = #UserId AND
da.[AssessmentType] = 'm' AND
da.[IdGroup] = #IdGroup AND
db.[IdCompetency] = e.[IdCompetency] AND
dd.[IdAssessment] = #IdAssessment
) AS TeamAverageAssessment
FROM
Spel_CM_AssessmentsData a
INNER JOIN Spel_CM_AssessmentsEvents c ON a.[IdAssessmentEvent] = c.[IdAssessmentEvent]
INNER JOIN Spel_CM_CompetenciesLevels d ON a.[IdCompetencyLevel] = d.[IdCompetencyLevel]
INNER JOIN Spel_CM_Competencies e ON d.[IdCompetency] = e.[IdCompetency]
INNER JOIN Spel_CM_Levels f ON d.[IdLevel] = f.[IdLevel]
-- This will link with user's assigned functions
INNER JOIN Spel_CM_FunctionsCompetenciesLevels g ON a.[IdFunction] = g.[IdFunction]
INNER JOIN Spel_CM_CompetenciesLevels h ON g.[IdCompetencyLevel] = h.[IdCompetencyLevel] AND e.[IdCompetency] = h.[IdCompetency]
INNER JOIN Spel_CM_Levels i ON h.[IdLevel] = i.[IdLevel]
WHERE
(NOT c.[EndDate] IS NULL) AND
a.[EvaluatedUserId] = #UserId AND
c.[IdAssessment] = #IdAssessment AND
a.[IdGroup] = #IdGroup
GROUP BY
e.[IdCompetency],
e.[Name],
i.[LevelNumber]
ORDER BY
e.[Name] ASC
-- This will define the position of each element (managers, group, self and team)
SELECT #intManagersPosition = #intMyCounter FROM #tblTotalsSource WHERE NOT ManagersAverageAssessment IS NULL
IF IsNumeric(#intManagersPosition) = 1 BEGIN SELECT #intMyCounter += 1 END
SELECT #intGroupPosition = #intMyCounter FROM #tblTotalsSource WHERE NOT GroupAverageAssessment IS NULL
IF IsNumeric(#intGroupPosition) = 1 BEGIN SELECT #intMyCounter += 1 END
SELECT #intSelfPosition = #intMyCounter FROM #tblTotalsSource WHERE NOT SelfAssessment IS NULL
IF IsNumeric(#intSelfPosition) = 1 BEGIN SELECT #intMyCounter += 1 END
SELECT #intTeamPosition = #intMyCounter FROM #tblTotalsSource WHERE NOT TeamAverageAssessment IS NULL
-- This will render the final table for the end user. The tabe will flatten some of the numbers to allow them to be prepared for Google Graphics.
SELECT
SUBSTRING(
(
SELECT
( '|' + REPLACE(ma.[CompetencyName],' ','+'))
FROM
#tblTotalsSource ma
ORDER BY
ma.[CompetencyName] DESC
FOR XML PATH('')
), 2, 1000) AS 'CompetenciesNames',
SUBSTRING(
(
SELECT
( ',' + REPLACE(ra.[FunctionRequiredLevel]*10,' ','+'))
FROM
#tblTotalsSource ra
FOR XML PATH('')
), 2, 1000) AS 'FunctionRequiredLevel',
SUBSTRING(
(
SELECT
( ',' + CAST(na.[ManagersAverageAssessment]*10 AS nvarchar(10)))
FROM
#tblTotalsSource na
FOR XML PATH('')
), 2, 1000) AS 'ManagersAverageAssessment',
SUBSTRING(
(
SELECT
( ',' + CAST(oa.[GroupAverageAssessment]*10 AS nvarchar(10)))
FROM
#tblTotalsSource oa
FOR XML PATH('')
), 2, 1000) AS 'GroupAverageAssessment',
SUBSTRING(
(
SELECT
( ',' + CAST(pa.[SelfAssessment]*10 AS nvarchar(10)))
FROM
#tblTotalsSource pa
FOR XML PATH('')
), 2, 1000) AS 'SelfAssessment',
SUBSTRING(
(
SELECT
( ',' + CAST(qa.[TeamAverageAssessment]*10 AS nvarchar(10)))
FROM
#tblTotalsSource qa
FOR XML PATH('')
), 2, 1000) AS 'TeamAverageAssessment',
SUBSTRING(
(
SELECT
( '|t++' + CAST([FunctionRequiredLevel] AS varchar(10)) + ',' + #RequiredColor + ',0,' + CAST(ROW_NUMBER() OVER(ORDER BY CompetencyName) - 1 AS varchar(2)) + ',9')
FROM
#tblTotalsSource
FOR XML PATH('')
), 2, 1000) AS 'FunctionRequiredAverageLabel',
SUBSTRING(
(
SELECT
( '|t++' + CAST([ManagersAverageAssessment] AS varchar(10)) + ',' + #ManagersColor + ',' + CAST(#intManagersPosition AS varchar(2)) + ',' + CAST(ROW_NUMBER() OVER(ORDER BY CompetencyName) - 1 AS varchar(2)) + ',9')
FROM
#tblTotalsSource
FOR XML PATH('')
), 2, 1000) AS 'ManagersLabel',
SUBSTRING(
(
SELECT
( '|t++' + CAST([GroupAverageAssessment] AS varchar(10)) + ',' + #GroupColor + ',' + CAST(#intGroupPosition AS varchar(2)) + ',' + CAST(ROW_NUMBER() OVER(ORDER BY CompetencyName) - 1 AS varchar(2)) + ',9')
FROM
#tblTotalsSource
FOR XML PATH('')
), 2, 1000) AS 'GroupLabel',
SUBSTRING(
(
SELECT
( '|t++' + CAST([SelfAssessment] AS varchar(10)) + ',' + #SelfColor + ',' + CAST(#intSelfPosition AS varchar(2)) + ',' + CAST(ROW_NUMBER() OVER(ORDER BY CompetencyName) - 1 AS varchar(2)) + ',9')
FROM
#tblTotalsSource
FOR XML PATH('')
), 2, 1000) AS 'SelfLabel',
SUBSTRING(
(
SELECT
( '|t++' + CAST([TeamAverageAssessment] AS varchar(10)) + ',' + #TeamColor + ',' + CAST(#intTeamPosition AS varchar(2)) + ',' + CAST(ROW_NUMBER() OVER(ORDER BY CompetencyName) - 1 AS varchar(2)) + ',10')
FROM
#tblTotalsSource
FOR XML PATH('')
), 2, 1000) AS 'TeamLabel',
(Count(src.[IdCompetency]) * 30) + 100 AS 'ControlHeight'
FROM
#tblTotalsSource src
SET NOCOUNT OFF
GO
Search your stored procedure for instances of this string: += (you have a few of them). This combined operator is not supported by T-SQL,
So instead of this:
SELECT #intMyCounter += 1
you will need to do this instead:
SELECT #intMyCounter = #intMyCounter + 1
when you run your create procedure script in sql server management studio and you get an error, double click on the error message and it usually takes you to the line where the error occurs. Sometimes this is just the first line of a long SELECT query, but in this case it is fairly obvious, because it is a one line IF
you can not use += in the line:
IF IsNumeric(#intManagersPosition) = 1 BEGIN SELECT #intMyCounter += 1 END
should be:
IF IsNumeric(#intManagersPosition) = 1 BEGIN SELECT #intMyCounter = #intMyCounter+1 END
you do this three times, fix then and you are good to go.
don't you miss BEGIN and END in the beginnning and eding of procedure, respectively?
(create procedure blablabla as begin .... end)
If SQL isn't giving you a line number, try commenting out different statements and re-running the command