Wrox Programmer Forums
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 9th, 2007, 07:37 AM
Authorized User
 
Join Date: Nov 2006
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to sanjivbshinde
Default Dynamic SQL

Hello Friends,
In Sql I have faced one problem.
I have cretaed two tables

Table1
SrNo. Allowance Prompt Fromdate Todate
1 Allowance1 HRA 12/30/2006 NULL
2 Deduction5 PF 12/30/2006 NULL

Table2
SrNo EmpId Allowance1 Allowance2 Deduction1 Deduction2 Deduction5
1 1 0.0 100 0.0 0.0 50
2 2 10.0 0.0 10.0 0.0 100.0


these are my two tables with some data.

Now I want to find value of PF for employee 2

for that i write one stoered procedure which takes employee id as parameter

create proc ss
@EmpId int
as
declare @ADType varchar(30)
declare @sql varchar(1000)

select @ADType=Allowance from Table1 where prompt='PF' and Todate is null
set @sql='select ' + @ADType + ' from Table2 where Empid= ' + Cast(@EmpId as varchar(50))
exec(@sql)
GO


this is what is my stoered procedure.
If i run it it gives me correct output
as

Deduction5
100.0



but i m unable to access this procedure from ASP.net as it contains dynamic sql.
for that i have to fill Dataset but dataset can't fill with that one column
insted it shows zero tables as well as zero columns.
But my requirement is that i have to fill that Dataset with that column.
So plze tell solution for that.

Thanks,
Sanjiv shinde.

 
Old January 9th, 2007, 11:45 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You should consider a redesign of your table #2. Normalization rules would state that table2 is formed improperly because you have multiple columns that represent the same kind of data (i.e. you have 2 Allowance columns and 3 deduction columns).

Instead, build the table with a column that defines the type of data in the row (allowance OR deduction) and a column that lists which of them it is. This also permits expanding the data without changing the table (i.e. added a 3rd allowance or a 4th deduction) because you just create another key value. Here's what it could look like:
Code:
Table2
SrNo EmpId RowType TypeNum Amount
1     1     1       1       0.0
1     1     1       2       100
1     1     -1      1       0.0
1     1     -1      2       0.0
1     1     -1      5       50
2     2     1       1       10.0
2     2     1       2       0.0
2     2     -1      1       10.0
2     2     -1      2       0.0
2     2     -1      5       100.0
I chose 1 and -1 because you could use that directly in SQL to perform math against the value in the "Amount" column. "Amount * RowType" gives you a value to add to whatever you need to add it to. The -1 results in a "deduction" (such as -100).

The additional columns "RowType" and "TypeNum" added to "SrNo" and "EmpId" result in a 4 column key to the table. Then your stored procedure can always reference the same column regardless of whether you want an Allowance (RowType=1) or Deduction (RowType=-1).

This isn't a complete solution to your problem, but I hope it provides a suggestion towards normalization of the data that will lead to an easier stored procedure and less pain later on.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help w/ dynamic SQL statement ssmith001 Perl 1 February 23rd, 2007 10:13 AM
C# Sql dynamic update N4th C# 2005 0 December 20th, 2006 09:05 AM
Dynamic SQL jbenson001 SQL Server 2000 4 January 12th, 2005 11:14 PM
dynamic-sql-cursor uskiranj Oracle 1 July 5th, 2004 05:54 AM
Dynamic SQL Tina Pro VB.NET 2002/2003 0 April 25th, 2004 07:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.