0

I have a query that I try in Microsoft SQL Server Management Studio, and this query work fine:

DECLARE @tableVariable TABLE (fecha DATE, valor Float);
INSERT @tableVariable SELECT fecha, valor FROM table1;
SELECT * FROM @tableVariable

When I go to VisualStudio and paste this query in sqldatasource, not work.

Anybody can help me?

Thanks

SOLVED

Finally, the problem is solved remove the tag <SelectParameters> <asp:Parameter Name = "tableVariable" /> </SelectParameters> into sqldatasource.

Thanks to all!

6
  • why don't you just paste SELECT fecha, valor FROM table1 in SqlDataSource? I don't think it supports multiple SQL statements. Commented Jan 30, 2013 at 8:22
  • The real query is very long for put here, and very complex. It is an abbreviated example. Must be some way to be able to run in asp.net. I use sqldatasource for show the result in a GridView. Commented Jan 30, 2013 at 8:30
  • When I put this in my sqldatasource for it is asking me to Define Parameters, and @tableVariable is not a parameter in my application, but a temp table in sql statement. Commented Jan 30, 2013 at 8:35
  • in this case, just encapsulate the complex query in a stored procedure and use that in the data source Commented Jan 30, 2013 at 8:35
  • How i can encapsulate the complex query? Any example? Thanks! Commented Jan 30, 2013 at 8:41

3 Answers 3

3

Try creating a stored procedure, like so:

CREATE PROCEDURE ComplexQuery
as
    DECLARE @tableVariable TABLE (fecha DATE, valor Float);
    INSERT @tableVariable SELECT fecha, valor FROM table1;

    -- more complex logic in here

    SELECT * FROM @tableVariable

go

Then, in your data source specify the name of the procedure.

Example:

        <asp:SqlDataSource
            id="SqlDataSource1"
            runat="server"
            ConnectionString="..."
            SelectCommandType="StoredProcedure"                
            SelectCommand="ComplexQuery">
        </asp:SqlDataSource>
Sign up to request clarification or add additional context in comments.

3 Comments

Can I pass arguments (parameters) to a stored procedure? It is possible drop and insert rows dynamically? When I call to ComplexQuery, I need pass arguments o parameters.
@user2024475 sure, see the stored procedure link I posted for details on how to add parameters to a stored proc. Then, specify the parameters in the data source as explained here: msdn.microsoft.com/en-us/library/z72eefad(v=vs.100).aspx
Oh Thanks you very much! This is the best answer for my problem. I haven't got reputation and I can't click to "answer useful". Thanks you very much again!
1

Is it not worth just converting the the declared table to a temp table i.e.

CREATE TABLE #tableVariable (fecha DATE, valor Float)

You would need a DROP TABLE #tableVariable later to make sure you are cleaning memory but it will have the desired affect I believe.

1 Comment

This option is computationally heavier, no?
0

I don't think you should be putting a long and complex query into SqlDataSource - that's really for binding a table or view to a datagrid without having to mess around in code behind. If you want to run a complex query you should either set up a stored procedure to do it and call that from SqlCommand, or put the text for the query into SqlCommand. Either way I don't think SqlDataSource is the way to go here.

2 Comments

I don't need a Stored Procedure. If I put the text of query into SqlCommand, I can use a GridView for show the result?
All the SqlCommand is going to do is run a SQL statement against the database. If the statement is going to return some data (SELECT * FROM...) then you still need to get the results of that into something that you can bind to a GridView like a DataSet or DataTable. Maybe SqlDataAdapter is the way to go here - you can feed it some SQL and easily generate a DataSet from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.