for instance i need to find value of storyId from one table then make sure i do not pull that record from another table. is this possible
With SQL you can do something like this:
select * from TableB where StoryIdValue not in (select StoryIdValue from TableA)
This is just 1 of many possible solutions.
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Yes this is possible. This is what is called a "Nested Query" in SQL.A SQL nested query is a SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE SQL query. Here is a simple example of SQL nested query:SELECT Model FROM Product
WHERE ManufacturerID IN (SELECT ManufacturerID FROM Manufacturer
WHERE Manufacturer = 'Sony')
(pls mark as answer if reply helps)
.NET Interviews & FAQs @ www.dotnetUncle.com
Click Mark as Answer if the reply helped you.
If you do want to keep the queries seperated from each other, you could turn them both into stored procedures. The first stored procedure will return a value, and you can define that value as an input-parameter of the second stored procedure.
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Thanks for your help Wim, could you provide an example of what the sqldatasource would look like with this input parameter in?
select name from emp where sal < (select max(sal) from emp)
here in this case i m going to select the name of the employee whose salary is less than the maximum sal
Plz mark as ANSWER, if my POST help u
jolyon:Thanks for your help Wim, could you provide an example of what the sqldatasource would look like with this input parameter in?
The example below is just fictive. So copy-pasting the code into your code will most likely not work!
You can use it as an example tough.
What I did is the following:
- I instantiate a connection
- I use that connection for 2 sqlCommands I want to run agianst the connection.
- The first sqlCommand executes a SP (stored procedure) which returns only one result. You have to make sure only one result is returned from your SP, or otherwise don't use the ExecuteScalar but something else, like the executeReader.
- Then the returnValue of the first Stored Procedure is stored in an Integer.
- That value is used in the second sqlCommand, which is also a stored Procedure.
- The resultset of the 2nd stored procedure is stored in a DataTable.
Ok, you have to keep a few things in mind:
EDIT: (and here is the example offcourse :) )
Partial Class Default9
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim connection As String = "Data Source=(local);Initial Catalog=Northwind;Integrated Security=True"
Dim myConnection As New SqlConnection(connection)
Dim dt As DataTable = New DataTable
'I'm sure thatmyFirstStoredProcedure will only return one result.
'That's why I can use ExecuteScalar!
Dim myCommand1 As New SqlCommand("myFirstStoredProcedure", myConnection)
myCommand1.CommandType = CommandType.StoredProcedure
Dim myStoryId As Integer = Convert.ToInt32(myCommand1.ExecuteScalar())
Dim myCommand2 As New SqlCommand("mySecondSP", myConnection)
myCommand2.CommandType = CommandType.StoredProcedure
'the code below which is commented out is a longer way of defining the parameter.
'Dim group As New SqlParameter("@group", SqlDbType.VarChar, 15)
'group.Direction = ParameterDirection.Input
'group.Value = GroupValue
Dim reader As SqlDataReader = myCommand2.ExecuteReader(CommandBehavior.CloseConnection)
I hope this answers your question.
If not please let me know!