Avatar of cesemj
cesemj

asked on 

How to modify SQL server 2005 query syntax to run in visual studio when I click a button.

Visual Studio 2005
vb.net

Hi
I use the following sql syntax manually in a sql server query to identify records in a table that have the same task description.  If the task description occours within a certain time period then it is a replica task and if it doe not occour in the same time period it is related task.

Problem:
I want the the syntax to run and update the records in a table when I click a button that saves the data to the data base.  I added the syntax right after the cmd.CommandText = but Visual studio treates the statement differently and does not like like the way the statement is sturcted on multiple lines.  I tried placing all the lines on on row, but it treates this section like a comment.  

Please suggest a way to get this sql statement to work in visual studio.

update t
set t.replica = Case When b.Description='replica' THEN 1 ELSE 0 END,
    t.related = CASE WHEN b.Description='related' THEN 1 ELSE 0 END
from
tblTask t
join
(
SELECT     Description = CASE WHEN ranking1 = 1 AND ranking2 = 1 THEN '' WHEN ranking1 > 1 AND ranking2 > 1 THEN 'replica' WHEN ranking1 = 1 AND
                      ranking2 > 1 THEN 'related' END, TaskID

FROM         (SELECT     ranking1 = row_number() OVER (partition BY Task, Occurrence
                       ORDER BY Task ASC), ranking2 = dense_rank() OVER (partition BY Task
ORDER BY TaskID ASC), *
FROM         tblTask) a
) b on t.TaskID = b.TaskID"
behind aspx page
 
saveData_onclick
 
Dim cmd As New SqlClient.SqlCommand()
cmd.Connection = New SqlConnection(Me.SqlDataSource1.ConnectionString)
cmd.Connection.Open()
 
 
cmd.CommandText = "update t
set t.replica = Case When b.Description='replica' THEN 1 ELSE 0 END,
    t.related = CASE WHEN b.Description='related' THEN 1 ELSE 0 END
from
tblTask t
join
(
SELECT     Description = CASE WHEN ranking1 = 1 AND ranking2 = 1 THEN '' WHEN ranking1 > 1 AND ranking2 > 1 THEN 'replica' WHEN ranking1 = 1 AND
                      ranking2 > 1 THEN 'related' END, TaskID
 
FROM         (SELECT     ranking1 = row_number() OVER (partition BY Task, Occurrence
                       ORDER BY Task ASC), ranking2 = dense_rank() OVER (partition BY Task
ORDER BY TaskID ASC), *
FROM         tblTask) a
) b on t.TaskID = b.TaskID"
 
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
cmd.Connection.Close()
GridView1.DataBind()

Open in new window

.NET ProgrammingMicrosoft SQL Server 2005Visual Basic.NET

Avatar of undefined
Last Comment
cesemj
SOLUTION
Avatar of chapmandew
chapmandew
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of magicclaw
magicclaw
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of cesemj
cesemj

ASKER

Thank you all,

I am going to look into creating a stored procedure in visual studio and will let you know what happens.

Thank you again for the direction.
Avatar of cesemj
cesemj

ASKER

I found this article on the internet and I am using it to help me work with and create stored Procedures.

Programming with Stored Procedures in Visual Basic .NET (Part 1)
http://www.developer.com/net/vb/article.php/1560751

Thank you again.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo