How to write the Stored Procedure - sql-server

I am having one situation,
Book Number | Book Authors | Publications | Versions
Controls i used in above fields, label in Book No., Combo box in Book Authors, Label in Publications and Combo box in Versions.
The above is my UI, if i choose the book authors from the combo box(values in combo box are retrieving from db), the values of publications and versions should retrieve from the db based upon the item i choose from the combo box. The page should not refresh. How to write the stored procedure for this.

It seems like you really are asking the wrong question. There are a couple good answers here for how to write a select statement (via a stored procedure, or not).
However, getting the data from the database has little to do with to putting that data into the appropriate controls on your UI, and nothing to do with making sure the page does not refresh.
If you are really interested in how to how to write a stored procedure for a simple select statement, accept #MikaelEriksson's answer, but add appropriate SET statements to minimize future upgrade issues. You will also need to modify the column and table names to reflect your actual data (of course).
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE GetBooksByAuthorID
#AuthorID int
AS
BEGIN
SELECT B.BookName,
B.BookID
FROM Books as B
WHERE B.AuthorID = #AuthorID
ORDER BY B.BookName
END
GO
If you are interested in how to bind the data to your UI, and how to do it without refreshing the UI; then you need to ask a new question about that. It should be specific to your web framework. Generally speaking, if you want web UI update without refresh, you will be using some form of AJAX.

If what you're trying to do is just cascading boxes, you write a SELECT query that return the appropriate rows. This is not what a stored procedure does.
SELECT * FROM `books` WHERE `author_id` = [author_id]
Stored procedures are used to process data, not select it.

Check this Sites to Start of:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET
Calling Stored procedures in ADO.NET
The C# Station ADO.NET Tutorial
Regards

This is how you write a stored procedure that will fetch something for you. I have of course no idea what output you want and what parameters you need so ...
create procedure GetBooksByAuthorID
#AuthorID int
as
select B.BookName,
B.BookID
from Books as B
where B.AuthorID = #AuthorID
order by B.BookName

Related

"The selected stored procedure or function returns no columns" when the query uses FREETEXTTABLE

I have the following stored procedure in an SQL Server database...
create procedure SupportTicketsFullTextSearch
#SearchText varchar(1000)
as
begin
select ID, k.rank
from SupportTicketsSummaries st
inner join freetexttable(SupportTicketsSummaries, (ShortSummary, Description), #SearchText) as k on st.ID=k.[key]
order by rank desc
end
I want to use this from Entity Framework, but when I try to add a function import, if I click the button to get the column information, I get the message "The selected stored procedure or function returns no columns"
I've done some searching, and seen a lot of people with the same problem, but they all seem to be using dynamic queries or temporary tables. My query doesn't use either (as far as I am aware anyway), and I can't find any advice that helps.
I tried adding...
SET FMTONLY OFF
...at the beginning of the SP, but it didn't help.
Anyone any ideas how I can import this SP as a function?
Never found out why the import wizard couldn't see the schema, but it turned out to be really easy to fix.
All you need to do is open the model browser, right-click on the Complex Types node, and add a new complex type that has properties that match the columns returned by your stored procedure. Make sure the names match exactly.
Then start the function import wizard, choose complex type, and pick the one you just created from the drop-down list.
Hope this helps someone.

Can a RDLC with a StoredProcedure dataset handle OUTPUT Parameter

I have many RDLC reports based on a StoredProcedure dataset on SqlServer.
My StoredProc have an OUTPUT parameter which receive an XML string with a list of ids to filter my table <ListXML><ItemId>10</ItemId><ItemId>11</ItemId></ListXML>.
I also want to show on my report header the formatted list of ids so users can see what they sent 10, 11.
So far, i'm handling the case with a CustomCode in the report properties using the System.Xml reference but i don't like having to use an external reference (especially while deploying the report on the server), the overhead of copy-pasting the code in each and every report and i'm concerned that my team will forget to copy the custom code and develop other solutions to handle the problem. I'd rather have my StoredProcedure return me the formatted string of Id to display on my report header.
i tested the case with a dummy sp
CREATE PROCEDURE [dbo].[Rep_Test]
#test VARCHAR(20) OUTPUT
AS
BEGIN
set #test = 'hello'
select top 10 * from item
END
and it revealed to me that it doesn't work right of the bat. Visual Studio does set 'Hello' as 'default value' to my parameter in the report but if i give a value to my report parameter it will not be overidden by the stored procedure.
Any insight on the subject or any other solution to handle the problem would be greatly appreciated
No. An RDLC cannot handle OUTPUT parameters in its dataset. You have to use a workaround.
One workaround is to employ a wrapper procedure that reads the resultset of the inner procedure, and adds another column with the value of the output parameter in every row.
A more detailed description with code can be found here.

How to display results from Stored Procedure WinForms

I have a stored procedure in SQL Server that returns different structures based on the parameters.
In other words, it might return a result set with three fields, or 15, and the column names are going to be different.
How can I display these results in a WinForm app?
I currently use the Entity Framework for accessing data, but obviously that is not going to work in this situation.
The data will be readonly, ie, no need to edit. Just need to display it.
I am guessing that I need to skip EF and just call the SP directly, and populate a DataGridView with autocolumns.
Greg
Since you're retrieving a dynamic data structure you'll need to make your grid columns dynamic too. What I would do is, after retrieving the result set successfully, to clear the Columns collection and recreate them from scratch every time you called the SP and the data needs to be refreshed on-screen. Do not use AutoGenerateColumns, as they provide little chance to customize the look of them, instead define each and every column yourself so that you can choose what to display and how.
This is the best answer I found: How to use a DataAdapter with stored procedure and parameter
In short, use a DataAdapter and a DataTable. Even with AutoGenerateColumns, the column headers look great and it works no matter the table structure that comes back.
Greg

How can I call a stored procedure without table names in HQL?

I am trying to fetch the current timestamp through a stored procedure in HQL. This means my code looks something like the following:
var currentTimestamp =
session.CreateQuery("SELECT CURRENT_TIMESTAMP()")
.UniqueResult<DateTime>();
This doesn't work. Specifically, it throws a System.NullReferenceException deep inside of the NHibernate HqlParser.cs file. I played around with this a bit, and got the following to work instead:
var currentTimestamp =
session.CreateQuery("SELECT CURRENT_TIMESTAMP() FROM Contact")
.SetMaxResults(1)
.UniqueResult<DateTime>();
Now I have the data I want, but an HQL query I don't. I want the query to represent the question I'm asking -- like my original format.
An obvious question here is "Why are you using HQL?" I know I can easily do with this session.CreateSQLQuery(...), hitting our MySQL 5.1 database directly. This is simply an example of my core problem, with the root being that I'm using custom parameter-less HQL functions throughout my code base, and I want to have integration tests that run these HQL parameter-less functions in as much isolation as possible.
My hack also has some serious assumptions baked in. It will not return a result, for example, if there are no records in the Contact table, or if the Contact table ceases to exist.
The method to retrieve CURRENT_TIMESTAMP() (or any other database function) outside of the context of a database table varies from database to database - some allow it, some do not. Oracle, for example, does not allow a select without a table, so they provide a system table with a single row which is called DUAL.
I suspect that NHibernate is implementing in HQL primarily features which are common across all database implementations, and thus does not implement a table-less select.
I can suggest three approaches:
Create a view that hides the table-less select such as 'create view dtm as select current_timestamp() as datetime'
Follow the Oracle approach and create a utility table with a single row in it that you can use as the table in a select
Create a simple stored procedure which only executes 'select current_timestamp()'

how to establish connection to database using WPF, C# and XAML

I started developing simple application in WPF and XAML.
I want to try accessign the sql server database and display the data from stored procedure onto the UI form.
I have a table called parentProject -> idParentProject (pk), txtParentProjName varchar(max).
my SP is parentProj_sp -> select * from parentProject.
n
I want to use a dropdown list in which, If the sp returns say (3) records, then dropdownlist should have those 3 records retreived from sp.
similar manner, I have subproject -> idsubproject(pk), idParentProject , txtSubProjectName varchar(max), dateProjstart, dateprojectend.
once the parentprojectname is selected from first dropdown, then, subproject dropdown should be enabled and it should have the subprojectnames for the corresponding parent project.
my sp is subproject_sp -> SELECT dtProjectStart, dtProjectEnd FROM tblSubProject WHERE (idParentProject = #idProjectIndex).
please help me how to connect to database in C# using WPF and XAML.
Thank You,
Ramm
1) Select the technology you want to use to access the database. (Hint: It's not WPF w/XAML)
2) Read the documentation for that particular technology.
3) Use the documented behavior to create your connection and do what you need to do.
4) Profit!
Your question clearly indicates that you don't understand the technology stack you're working with. You need to return to basics and grasp what WPF, XAML, and databases are, as well as how they interact.
Try
ScottGu's great series on Linq to Sql which is probably the easiest way to get started IMHO.
http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx
Or for more in depth study
http://msdn.microsoft.com/en-us/library/bb425822.aspx
You will want to combine two technologies. Data Acess and Data Binding.
For Data Acess use one of Ado.Net, Linq2Sql, or what the rest of your group prefers.

Resources