Missing space and closing )
protected void Page_Load(object sender, EventArgs e)
{
string dsn = "foo";
string sql = @"SELECT * FROM
(
SELECT F.Project AS 'Project Number', F.Account AS
'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS
'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom
C ON F.Project = C.Project
WHERE
F.Project LIKE '61000.003%' AND
F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate())
) Budget PIVOT
(
SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],
[11],[12])
)";
using (SqlConnection conn = new SqlConnection(dsn))
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
testGrid.DataSource = reader;
testGrid.DataBind();
}
}
But really you should create a stored procedure for something like this:
CREATE PROCEDURE [GetProjectBudgetInfo]
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM
(
SELECT F.Project AS 'Project Number', F.Account AS
'Account', F.Pd AS Period, F.Incurred AS Totals, C.Project AS
'Project Name' FROM Ultron.Final F INNER JOIN Ultron.Custom
C ON F.Project = C.Project
WHERE
F.Project LIKE '61000.003%' AND
F.Account NOT LIKE '%-01' AND F.Fy = YEAR(GetDate())
) Budget PIVOT
(
SUM(Totals) FOR Period in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
)
END
And then call it in code:
protected void Page_Load(object sender, EventArgs e)
{
string dsn = "foo";
string sql = @"GetProjectBudgetInfo";
using (SqlConnection conn = new SqlConnection(dsn))
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
testGrid.DataSource = reader;
testGrid.DataBind();
}
}
solved Sql: Incorrect syntax near ‘(‘ [closed]