Showing posts with label master. Show all posts
Showing posts with label master. Show all posts

Wednesday, March 28, 2012

Is this SQL stored prodcedure is valid

what i want to achive is the proc sh'd return a master-detail value in one go.
master value should be returned with Out Parameter and detail value as a recordset.

will it return the recordset of detail table as below...


Create Procedure ProductDetail
(
@.ProductID int,
@.ProductCode varchar(15) OUTPUT,
@.ProductName varchar(60) OUTPUT,
@.CategoryID int OUTPUT,
@.CategoryName varchar(60) OUTPUT,
@.Image1 varchar(256) OUTPUT,
@.Image2 varchar(256) OUTPUT,
@.UnitPrice smallmoney OUTPUT,
@.UOMValue numeric(9) OUTPUT,
@.UOMName varchar(10) OUTPUT,
@.ShippingWeight numeric(9) OUTPUT,
@.Directions varchar(1500) OUTPUT,
@.Ingrediants varchar(1500) OUTPUT,
@.Warnings varchar(1500) OUTPUT,
@.ShortDescription varchar(1000) OUTPUT,
@.LongDescription varchar(2000) OUTPUT,
@.NutritionFacts varchar(1000) OUTPUT,
@.SearchKeywords varchar(500) OUTPUT,
@.IsTaxable varchar(15) OUTPUT,
@.CreatedBy varchar(60) OUTPUT,
@.CreatedOn varchar(15) OUTPUT,
@.UpdatedBy varchar(60) OUTPUT,
@.UpdatedOn varchar(15) OUTPUT,
@.Status int OUTPUT
)
AS

SELECT
@.ProductCode = ProductCode,
@.ProductName = ProductName,
@.CategoryID = CategoryID,
@.CategoryName = (select CategoryName from mCategory where CategoryID=a.CategoryID),
@.Image1 = isnull(Image1,''),
@.Image2 = isnull(Image1,''),
@.UnitPrice = isnull(UnitPrice,0),
@.UOMValue = isnull(UOMValue,0),
@.UOMName = isnull(UOMName,''),
@.ShippingWeight = isnull(ShippingWeight,0),
@.Directions = isnull(Directions,''),
@.Ingrediants = isnull(Ingrediants,''),
@.Warnings = isnull(Warnings,''),
@.ShortDescription = isnull(ShortDesc,''),
@.LongDescription = isnull(LongDesc,''),
@.NutritionFacts = isnull(NutritionFacts,''),
@.SearchKeywords = isnull(SearchKeywords,''),
@.IsTaxable = case when isnull(IsTaxable,0)=0 then 'No' else 'Yes' End,
@.CreatedBy = isnull((select LName + ',' + FName from mUser where UserID=InsertedBy),''),
@.CreatedOn = InsertedOn,
@.UpdatedBy = isnull((select LName + ',' + FName from mUser where UserID=UpdatedBy),''),
@.UpdatedOn = UpdatedOn,
@.Status = Convert(int,isnull(Status,0))
FROM
mProduct a
WHERE
ProductID = @.ProductID

SELECT
ID as PricingDetailID,
isnull(PricingFromQnty,0) as PricingFromQnty,
isnull(PricingToQnty,0) as PricingToQnty,
isnull(RangePrice,0) as RangePrice,
Convert(int,isnull(Status,0))as Status
FROM
dProduct
WHERE
ProductID = @.CategoryID

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


Regards,
BhairavI believe the way you are doing it is possible, but why not return two recordsets back to a dataset? Then you would have a master datatable and detail datatable. I believe this would work:

Create Procedure ProductDetail

(

@.ProductID int
)

AS

SELECT
ProductCode,
ProductName,
CategoryID,
(select CategoryName from mCategory where CategoryID=a.CategoryID),
isnull(Image1,''),
isnull(Image1,''),
isnull(UnitPrice,0),
...

FROM

mProduct a

WHERE

ProductID = @.ProductID

SELECT

ID as PricingDetailID,

isnull(PricingFromQnty,0) as PricingFromQnty,

isnull(PricingToQnty,0) as PricingToQnty,

isnull(RangePrice,0) as RangePrice,

Convert(int,isnull(Status,0))as Status

FROM

dProduct

WHERE

ProductID = @.CategoryID

GO

SET QUOTED_IDENTIFIER OFF

GO

SET ANSI_NULLS ON

GO

HTH|||thks for u'r suggesion
but can u plz explain me in more detail ...
how my dataset code will look like when a single strore proc return more than one recordset.

proc must not be called more than once for that...

Regards,
Bhairav|||Sure,
Just follow the code (I'm using Microsoft Data Access Application Blocks to call):


SqlParameter [] arParms = new SqlParameter[1];
arParms[0] = new SqlParameter("@.ProductID", SqlDbType.Int);
arParms[0].Value = 1;

DataSet myDS = SQLHelper.ExecuteDataset("connectiion", StoredProcedure, "ProductDetail", arParms);
DataTable myTable1 = myDS.Tables[0];
DataTable myTable2 = myDS.Tables[1];

That should give you an example of the calling code. There are other ways to manipulate the dataset data. If you are unfamiliar, just hollar and we can give you some direction, or search the archives of the data access forms. HTH|||thks..
its really the nice way to code
thks again

Regards,
Bhairav

Monday, March 26, 2012

is this possible? ...not an SQL master

Hi,

I am trying to figure out the best way to reformat the record entries in a database.

In the source data table on the server, all field types have been defined as 'text' (for some reason), and I need to pull these data out and create a new table with appropriate datatype definitions for the fields.

Also, two fields are mixed alpha-numeric, while there should really be separate fields for the alpha values.

I have attached a copy of a screenshot with some notes.

Thanks in advance to anyone who sees the easy way to do this!

cheers,
RickAn example:

INSERT INTO newtable (county, route, px_back, backpm)
SELECT county, integer(route), CASE WHEN SUBSTR('098',LENGTH('098')) > 'A' THEN SUBSTR('098', 1, LENGTH('098') -1) ELSE null END, CASE WHEN SUBSTR('098R',LENGTH('098R')) > 'A' THEN SUBSTR('098R', LENGTH('098R')) ELSE null END FROM oldtable

Assumes that the character is always the last position and length of 1.

Originally posted by entangled
Hi,

I am trying to figure out the best way to reformat the record entries in a database.

In the source data table on the server, all field types have been defined as 'text' (for some reason), and I need to pull these data out and create a new table with appropriate datatype definitions for the fields.

Also, two fields are mixed alpha-numeric, while there should really be separate fields for the alpha values.

I have attached a copy of a screenshot with some notes.

Thanks in advance to anyone who sees the easy way to do this!

cheers,
Rick|||HI,

try to use decode(substr(backPM,length(backPM)-1,1),'R',substr(backPM,1,length(backPM)-1,backPM)

and use the same formula for AheadPM column

Originally posted by entangled
Hi,

I am trying to figure out the best way to reformat the record entries in a database.

In the source data table on the server, all field types have been defined as 'text' (for some reason), and I need to pull these data out and create a new table with appropriate datatype definitions for the fields.

Also, two fields are mixed alpha-numeric, while there should really be separate fields for the alpha values.

I have attached a copy of a screenshot with some notes.

Thanks in advance to anyone who sees the easy way to do this!

cheers,
Rick|||Thank you for the example. This one example pretty much addresses both issues. I will work with this and see how I can apply this approach.

many thanks,
Rick Sperling

Originally posted by dmmac
An example:

INSERT INTO newtable (county, route, px_back, backpm)
SELECT county, integer(route), CASE WHEN SUBSTR('098',LENGTH('098')) > 'A' THEN SUBSTR('098', 1, LENGTH('098') -1) ELSE null END, CASE WHEN SUBSTR('098R',LENGTH('098R')) > 'A' THEN SUBSTR('098R', LENGTH('098R')) ELSE null END FROM oldtable

Assumes that the character is always the last position and length of 1.

Friday, March 23, 2012

is this possible

hey all,
what's the best way to get all the records in a master table and a sum of a
column in a related details table?
thanks,
rodcharThe best way is to post DDL and sample data (
http://www.aspfaq.com/etiquette.asp?id=5006 )
But...
SELECT Master.PKCol, SUM(Detail.SomeCol) SumSomeCol
FROM Master
JOIN Detail ON Master.PKCol = Detail.PKCol
GROUP BY Master.PKCol
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
a
> column in a related details table?
> thanks,
> rodchar|||without seeing the ddl, this is a guess.
select m.id,sum(c.col)
from master m left join child c on m.id=c.fk
group by m.id
-oj
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
> a
> column in a related details table?
> thanks,
> rodchar|||Well...you query it for the things you need and sum the column that gives yo
u
the answer. Once you have it, it will be obvious how it should be assemble t
o
get those things you need.
Vagueness begets vague answers. Post DDL
Thomas
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
a
> column in a related details table?
> thanks,
> rodchar|||SELECT <col_list_from_master_table>,
(SELECT SUM(<col_name> )
FROM details_table AS D
WHERE D.referencing_col = M.referenced_col) AS sumdetail
FROM master_table AS M
BG, SQL Server MVP
www.SolidQualityLearning.com
"rodchar" <rodchar@.discussions.microsoft.com> wrote in message
news:D309581C-EBC3-4AA9-A5BC-91AEA5FE88FF@.microsoft.com...
> hey all,
> what's the best way to get all the records in a master table and a sum of
> a
> column in a related details table?
> thanks,
> rodchar

Is this error something to worry about?

Hi Everyone,
One of my clients started getting error messages in their "Database
Maintenance Plan" that has me concerned.
Databases: master & msdb (two error messages but same codes)
Activity: Check Data and Index Linkage
Error number: 7919
Message: Repair statement not processed. Database needs to be in single
user mode.
I checked Microsoft knowledgebase and it appears that this is a known issue
http://support.microsoft.com/default...;en-us;Q290622
What concerns me is this never happened before to any of my clients and
it seems serious as it affects key system databases Master and msdb.
Should I be worried?
Thanks
Richard
hi Richard,
Richard Fagen wrote:
> ...
> What concerns me is this never happened before to any of my clients
> and
> it seems serious as it affects key system databases Master and msdb.
> Should I be worried?
hopefully not, but you actually and unfortunately have no workaround for
that...
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.11.1 - DbaMgr ver 0.57.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply
|||Hi Andrea,
I suspected as much, but I wanted confirmation from the experts
Maybe I'll convince them to upgrade to SQL 2005 later in the year.
I know ISA 2004 will be included in SBS SP1 for free! (expected to be
almost 400M!) Do you think Microsoft would be generous with SQL 2005 or
maybe offer it at a reduced price for SBS 2003 users?
Thanks for your help
Richard
Andrea Montanari wrote:
> hi Richard,
> hopefully not, but you actually and unfortunately have no workaround for
> that...
|||hi Richard,
Richard Fagen wrote:
> Hi Andrea,
> I know ISA 2004 will be included in SBS SP1 for free! (expected to be
> almost 400M!) Do you think Microsoft would be generous with SQL 2005
> or maybe offer it at a reduced price for SBS 2003 users?
I suspect this is out of my concerns :D:D
Andrea Montanari (Microsoft MVP - SQL Server)
http://www.asql.biz/DbaMgr.shtmhttp://italy.mvps.org
DbaMgr2k ver 0.11.1 - DbaMgr ver 0.57.0
(my vb6+sql-dmo little try to provide MS MSDE 1.0 and MSDE 2000 a visual
interface)
-- remove DMO to reply

Friday, March 9, 2012

Is there anything like rowid, rownum like in MySql and Oracle?

Hi,

i am new to SQL Server. I want to write a query where in i want to delete duplicate rows from a table keeping the master copy.

If it is MySQL or Oracle we can write that using built in rownum or rowid. How to do that task in SQL Server 2005. Is there anything like rowid, rownum in SQL Server? If not suggest me a way to do that?

...aazad

not exactally , but u can make use of 'TOP' or row_number() function..

select top 1 from table 1 order by column1

u can use top intelligently to get top/bottom nth row... given u have somethin to orderby

|||Rownum is a psuedo column that generates a logical sequence number so it will change depending on the query execution plan, data etc. Rowid on the other hand is a physical identifier (at least in Oracle). So how are you using these in your queries? What is the purpose of using something like ROWID? You do have primary key or unique key constraints on your tables right! It will be easier to suggest the alternatives if we know your use cases.|||

Hi chandar,

There can be a senario where a table has no primary key and has data. Later when i want to make a column as primary key, i need to delete the duplicates, which i dont want to do it manually. so i shud write a query where i can delete duplicate rows keeping one copy of it. I worked with MySQL and in MySQL i can write a query as follows

delete from test where rowid in ( select rownum from test where rownum not in ( select min(rownum) from test group by all_columns having count(*) > 1 ) group by all_columns having count(*) > 1

The above code deletes the duplicates the master copy in MySQL. I am using that logical column rownum. How to do the same job in SQL Server 2005?

Regards..,

Aazad

|||

okk...lect us say u want to make column1 as ur primary key in table1 , so to find out the duplicate(or more) entries of this key , use the following query...

select column1 from table1

group by column1

having count(column1)>1

this will enlist all the entries for column1 which r repeating...

|||

Thank god .. you are using SQL Server 2005 use the following query

Example:

CREATE TABLE Table1

(

[Id] [int] NULL

)

go

INSERT INTO Table1 values(10);

INSERT INTO Table1 values(10);

INSERT INTO Table1 values(20);

INSERT INTO Table1 values(20);

go

With Test(rownum,ID)

as

(

Select Row_Number() OVER (ORDER BY ID), * From Table1

)

Delete From Test Where rownum in

(

Select A.rownum From Test A JOIN Test B On B.Id=A.ID and A.rownum >= B.rownum

Group bY A.rownum,A.ID Having Count(A.ID) <> 1

)

|||hi
use newid() function
good luck