Quantcast
Channel: Transact-SQL forum
Viewing all 12890 articles
Browse latest View live

Problem with Decimal Places and Rounding off

$
0
0
Good Day All 

i have a Following Query 

1) Query 1

[CODE]DECLARE @FINALVAUE VARCHAR(MAX) =   (Convert(decimal,@CAUSATIVE_FACTORS_ID) - 1) +   Convert(decimal,@LU_PERCENTAGE_FAILURE_IN_SAMPLE_ID) + Convert(decimal,@LU_REPORTING_HISTORY_ID)  
PRINT @FINALVAUE -- the answer is 3 [/CODE]

The Calculation is like this 

2) Query 2

[CODE]DECLARE @FINAL VARCHAR(MAX) 
SET  @FINAL=  (4-1) + 0.08 + 0.2
SELECT @FINAL -- the asnwer is 3.28 
[/CODE]
 

The first Query i am retrieving values from the Variables which are varchar and converting to decimal and doing the calculations. and the second Query i am just taking the value as is. i used this to debug in trying to solve this problem. My problem here in the first Query in the @FINALVAUE variable the answer is "3" and in the second Query the answer is "3.28"
, i want the first Query to display the same value which is "3.28"

Thanks
 

Vuyiswa Maseko


Optimising a Cursor

$
0
0

Hi,

For the following query the estimated execution plan identifies a missing index

/*
Missing Index Details from SQLQuery20.sql - WCCSQL.TicketorWCC_TEST (sa (107))
The Query Processor estimates that implementing the following index could improve the query cost by 85.2079%.
*/

/*
USE [MyDatabase]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[VEHICLE] ([ADDRCITY],[ADDRESS])
INCLUDE ([TICKETNUM],[AddrNoCrLf])
GO
*/

I created the index, refreshed the  query but it still displayed the missing index. I then added the index as a hint but that hasn't helped either.

The table function used in the query is on another table so the optimizer is not referring to that.

The query is

Declare @Address nvarchar(255)
Declare @Ticket nvarchar(10)
Declare @CityName nvarchar(50)
Declare mycursor cursor for
select  v.AddrNoCrLf,v.TICKETNUM from [TicketorWCC_TEST].dbo.vehicle v where ADDRESS is not null and len(ADDRESS) > 5 and ADDRCity is null
OPEN myCursor
FETCH NEXT from myCursor into @Address,@Ticket
WHILE @@FETCH_STATUS = 0
begin
    Select @CityName = (select top 1 [CityName] from dbo.fnGetCityName(@Address))
    update v1 set ADDRCITY = @CityName from [TicketorWCC_TEST].dbo.Vehicle v1 with (INDEX(ixAddressCityAddress)) where TICKETNUM = @Ticket
    FETCH NEXT FROM myCursor into @Address,@Ticket
end
CLOSE myCursor
DEALLOCATE myCursor

It is a very slow query anyway due to the function so anything I can do to improve it is greatly appreciated.


bob_clegg

MTD/YTD parameters to a Stored Procedure

$
0
0

Hi guys,

I am new to SQL.

I know there are plenty of YTD/MTD sql calculations but I have been unable to pass them any of them as parameters to may Stored Procedure.

If someone can help me solve at least two of them (let's say YTD/MTD). I will do the same for the rest of parameters (QTD/WTD) following the example. Please find my current SP below.

ALTER Procedure [dbo].[DIFOT_WH]
AS
Select  CORDDocs.DateOpn AS 'CORD_DateOpen', CORDDocs.DocumentCode as 'CORD_DocumentCode', CDSPDocs.DocumentCode AS 'DESP_DocumentCode', 
	Count(CORDLines.LneType) AS 'Cord_Lines', SUM(CORDLines.Qnty) AS 'CORD_Qty', CDSPLines.Status, Count(CDSPLines.Qnty) AS 'DESP_Lines', 
	Sum(CDSPLines.Qnty) AS 'DESP_Qty', CORDDocs.DateReq AS 'CORD_DateReq', CDSPDocs.PostDate AS 'DESP_PostedDate',  
CASE 
	When SUM(CORDLines.Qnty) = Sum(CDSPLines.Qnty) THEN 'IF' ELSE 'NOT IF'
	END as 'IFOT'
From dbo.Documents AS CORDDocs INNER JOIN
	dbo.DocumentsLines AS CORDLines ON CORDDocs.DocID = CORDLines.DocID AND CORDLines.TrnType = 'L' AND CORDLines.LneType <> 'Fght' LEFT OUTER JOIN
	dbo.DocumentsLines AS CDSPLines ON CORDLines.TranID = CDSPLines.SourceID AND CDSPLines.TrnType = 'L' AND (CDSPLines.Status = 'Posted' OR CDSPLines.Status = 'Closed') LEFT OUTER JOIN
	dbo.Documents AS CDSPDocs ON CDSPLines.DocID = CDSPDocs.DocID 
WHERE (CORDDocs.DocumentName = 'DocCord')
Group by CORDDocs.DateOpn, CORDDocs.DocumentCode, CDSPDocs.DocumentCode, CORDDocs.DateReq, CDSPDocs.PostDate, CDSPLines.Status
ORDER BY CORDDocs.DateOpn,'CORD_DocumentCode'

I appreciate all your help

Thanks

Eric


Common Table Expression Error

$
0
0

I have a requirement to rewrite some of our simple select statement queries using a CTE. And I have had no problems doing so until I encountered one from a query like the one below:

USE AdventureWorks2012
GO
DECLARE @TotalRows INT 
SELECT @TotalRows = COUNT(CustomerID)
FROM Sales.Customer
WHERE CustomerID IN (SELECT CustomerID FROM Sales.SalesOrderHeader);

SELECT @TotalRows AS 'T'

And I rewrote it like the one below but I am getting error.

DECLARE @TotalRows INT 
;WITH TotalRows
(
SELECT @TotalRows = COUNT(CustomerID)
FROM Sales.Customer
WHERE CustomerID IN (SELECT CustomerID FROM Sales.SalesOrderHeader))
SELECT * FROM TotalRows

How do I make this work using a CTE?



Zionlite

How can I group student scores into quintile using SQL Server 2008

$
0
0

Can anyone help me to group student scores into quintile. I think there is a feature in SQL Server 2012, but still we are have not upgrade to it as we are using 2008R2. I tried Ntile(5) but it not generating the desired result. I need below Quintile column

Student Score Quintile

Student1 20 1

Student2 20 1

Student3 30 2

Student4 30 2

Student5 40 2

Student6 40 2

Student7 50 3

Student8 50 3

Student9 60 3

Student10 70 4

Student11 70 4

Student12 80 4

Student13 80 4

Student14 90 5

SP_Rename to rename objects

$
0
0

Hi,

I have a table name like 'TEST_24-10-2013.[Employee_2006201_LeftBank]', I want to rename the Object without "[" and "]" in the t bale name. If i use the following syntax, it throws error

Note: the table name contains [ and ] at the  beginning and end of the name.

EXEC SP_RENAME '[TEST_24-10-2013].[[Employee_2006201_LeftBank]]','Employee_2006201'

Error:

Msg 15253, Level 11, State 1, Procedure sp_rename, Line 105

Syntax error parsing SQL identifier '[TEST_24-10-2013].[[Employee_2006201_LeftBank]]'.

Top-pest parent column should be as new column

$
0
0

Hi,

i have a table like below 

CREATE TABLE #MyTable (ID Integer,Description varchar(200),ParentID Integer)
insert INTO #MyTable (ID,Description,ParentID) VALUES (1,'1AAA',0)
insert INTO #MyTable (ID,Description,ParentID) VALUES (2,'1BBB',0)
insert INTO #MyTable (ID,Description,ParentID) VALUES (3,'2AAA',1)
insert INTO #MyTable (ID,Description,ParentID) VALUES (4,'2BBB',2)
insert INTO #MyTable (ID,Description,ParentID) VALUES (5,'3AAA',3)
insert INTO #MyTable (ID,Description,ParentID) VALUES (6,'3AAB',3)
insert INTO #MyTable (ID,Description,ParentID) VALUES (7,'3BBB',4)
insert INTO #MyTable (ID,Description,ParentID) VALUES (8,'3BBC',4)
SELECT * from #MyTable

I need a new column which has to display the top-pest parent. like below

Thanks

LOgin name

$
0
0

Hi Team,

Looking output on below with out duplicates

Loginname    Login permission

Request to you experts please provide the exact script.

Thanks in advance.


subu



SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

$
0
0
Here is sql Store procedure
Create procedure InsertResMasterNEW 
        @urm_id int output
 
        @upm_sector varchar(60) , @upm_block varchar(50) , @upm_plot varchar(50) , @urm_entry_dt datetime, 
        @urm_loc_adv varchar(50) ,@urm_alott_catg varchar(50) , @urm_grnd_rent numeric(18,0) , @urm_grnd_rent_pp varchar(50), 
        @urm_grnd_rent_desc varchar(50) ,   @urm_resi varchar(50) , @urm_shop_allowed varchar(50) , @urm_non_resi varchar(50), 
        @urm_yes_resi varchar(50) , @urm_allot_no varchar(50),  @urm_allot_dt datetime ,    @urm_reg_dt datetime, 
        @urm_exe_dt datetime , @urm_coop_name varchar(50) , @urm_coop_reg_no varchar(50) ,  @urm_org_cost numeric(18,0), 
        @urm_bp_cost numeric(18,0) ,    @urm_interest_paid numeric(18,0) ,  @urm_amount_paid numeric(18,0) ,    @urm_bal_due numeric(18,0), 
        @urm_poss_dt datetime , @urm_poss_cert_no varchar(50) ,  @urm_poss_type varchar(50) , @urm_grnd_rentupto_yr numeric(18,0) , 
        @urm_grnd_rent_paid numeric(18,0) , @urm_grnd_rent_bal numeric(18,0) ,  @urm_pln_submitted varchar(50) , @urm_pln_subm_org_dt datetime, 
        @urm_pln_subm_org_no varchar(50) ,  @urm_pln_subm_rem varchar(50) , @urm_pln_sanctioned varchar(50) , @urm_pln_sanc_org_dt datetime, 
        @urm_pln_sanc_no    varchar(50) ,   @urm_pln_sanc_rem varchar(50) , @urm_pln_sanc_rev_dt datetime , @urm_pln_sanc_rev_no varchar(50), 
        @urm_const_status varchar(50) , @urm_const_storeys varchar(50) , @urm_pln_storeys   varchar(50) , @urm_comp_storeys varchar(50) , 
        @urm_const_rem varchar(50) ,  @urm_comm_util varchar(50) , @urm_comm_util_other varchar(50) , @urm_pub_util varchar(50), 
        @urm_pub_util_other varchar(50) ,   @urm_ind_util1 varchar(50) , @urm_plot_lease_ownby varchar(50) , @urm_prev_alloted varchar(50), 
        @urm_whose_fav varchar(50) , @urm_tenyr_exp varchar(50) , @urm_tenyr_org_dt datetime , @urm_tenyr_org_no varchar(50), 
        @urm_tenyr_rev_dt datetime , @urm_tenyr_rev_no varchar(50) , @urm_tenyr_rem varchar(50) , @urm_cc_rem varchar(50) 
 
AS 
Declare @urm_upm_id int 
Begin 
select @urm_upm_id=upm_id from URB_PLOT_MASTER  
    where upm_sector=@upm_sector  
    and   upm_block=@upm_block  
    and   upm_plot=@upm_plot  
  
Insert into URB_RES_MASTER( urm_upm_id  , 
                            urm_entry_dt , urm_loc_adv , urm_alott_catg , urm_grnd_rent, 
                            urm_grnd_rent_pp,  urm_grnd_rent_desc, urm_resi,   
                            urm_shop_allowed,  urm_non_resi,  urm_yes_resi,  urm_allot_no, 
                            urm_allot_dt, urm_reg_dt,  urm_exe_dt,  urm_coop_name, 
                            urm_coop_reg_no, urm_org_cost, urm_bp_cost, urm_interest_paid,  
                            urm_amount_paid,  urm_bal_due,  urm_poss_dt,  urm_poss_cert_no,   
                            urm_poss_type, urm_grnd_rentupto_yr, urm_grnd_rent_paid,  urm_grnd_rent_bal,  
                            urm_pln_submitted,  urm_pln_subm_org_dt,  urm_pln_subm_org_no,  urm_pln_subm_rem, 
                            urm_pln_sanctioned, urm_pln_sanc_org_dt,  urm_pln_sanc_no, urm_pln_sanc_rem,  
                            urm_pln_sanc_rev_dt, urm_pln_sanc_rev_no,urm_const_status, urm_const_storeys,  
                            urm_pln_storeys,  urm_comp_storeys,  urm_const_rem,  urm_comm_util,   
                            urm_comm_util_other,  urm_pub_util, urm_pub_util_other, urm_ind_util1,   
                            urm_plot_lease_ownby, urm_prev_alloted, urm_whose_fav,  urm_tenyr_exp, 
                            urm_tenyr_org_dt, urm_tenyr_org_no, urm_tenyr_rev_dt, urm_tenyr_rev_no,  
                            urm_tenyr_rem, urm_cc_rem ) 
  
                    values( @urm_upm_id,   @urm_entry_dt, @urm_loc_adv ,  @urm_alott_catg , 
                            @urm_grnd_rent,  @urm_grnd_rent_pp,  @urm_grnd_rent_desc, @urm_resi,   
                            @urm_shop_allowed, @urm_non_resi,  @urm_yes_resi,  @urm_allot_no,  
                            @urm_allot_dt,  @urm_reg_dt,  @urm_exe_dt, @urm_coop_name,   
                            @urm_coop_reg_no ,  @urm_org_cost,  @urm_bp_cost,   
                            @urm_interest_paid,  @urm_amount_paid,  @urm_bal_due,  @urm_poss_dt, @urm_poss_cert_no,  
                            @urm_poss_type, @urm_grnd_rentupto_yr,   @urm_grnd_rent_paid, @urm_grnd_rent_bal, 
                            @urm_pln_submitted,   @urm_pln_subm_org_dt, @urm_pln_subm_org_no, @urm_pln_subm_rem , 
                            @urm_pln_sanctioned,  @urm_pln_sanc_org_dt, @urm_pln_sanc_no, @urm_pln_sanc_rem , 
                            @urm_pln_sanc_rev_dt, @urm_pln_sanc_rev_no, @urm_const_status, @urm_const_storeys,  
                            @urm_pln_storeys, @urm_comp_storeys, @urm_const_rem, @urm_comm_util,  
                            @urm_comm_util_other, @urm_pub_util, @urm_pub_util_other, @urm_ind_util1,  
                            @urm_plot_lease_ownby, @urm_prev_alloted, @urm_whose_fav, @urm_tenyr_exp,  
                            @urm_tenyr_org_dt, @urm_tenyr_org_no, @urm_tenyr_rev_dt, @urm_tenyr_rev_no,  
                            @urm_tenyr_rem, @urm_cc_rem ) 
End 
SELECT @urm_id=SCOPE_IDENTITY() 
 

And the c# from i  send the data to the store procedure .. and it throws me the problem ..

Server Error in '/LandMangement' Application.

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Source Error:

Line 134:                    strLastError = ex.Message;
Line 135: else
Line 136: throw ex;
Line 137: }
Line 138:

Source File: e:\LandMangement\App_Code\DAL\DBAccess.cs   Line: 136

Stack Trace:

[SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.]
SechBhaban.DAL.DBAccess.ExecuteNonQuery(String commandtext) in e:\LandMangement\App_Code\DAL\DBAccess.cs:136
SechBhaban.BOL.ResiMaster.Insert(String sector, String block, String plot, DateTime urm_entry_dt, String urm_loc_adv, String urm_alott_catg, Int32 urm_grnd_rent, String urm_grnd_rent_pp, String urm_grnd_rent_desc, String urm_resi, String urm_shop_allowed, String urm_yes_resi, String urm_non_resi, String urm_allot_no, DateTime urm_allot_dt, String urm_prev_alloted, String urm_whose_fav, DateTime urm_reg_dt, DateTime urm_exe_dt, String urm_coop_name, String urm_coop_reg_no, Int32 urm_org_cost, Int32 urm_interest_paid, Int32 urm_bp_cost, Int32 urm_amount_paid, Int32 urm_bal_due, DateTime urm_poss_dt, String urm_poss_cert_no, String urm_poss_type, Int32 urm_grnd_rentupto_yr, Int32 urm_grnd_rent_paid, Int32 urm_grnd_rent_bal, String urm_pln_submitted, DateTime urm_pln_subm_org_dt, String urm_pln_subm_org_no, String urm_pln_subm_rem, String urm_pln_sanctioned, DateTime urm_pln_sanc_org_dt, String urm_pln_sanc_no, DateTime urm_pln_sanc_rev_dt, String urm_pln_sanc_rev_no, String urm_pln_sanc_rem, String urm_tenyr_exp, DateTime urm_tenyr_org_dt, String urm_tenyr_org_no, DateTime urm_tenyr_rev_dt, String urm_tenyr_rev_no, String urm_tenyr_rem, String urm_pln_storeys, String urm_const_status, String urm_comp_storeys, String urm_const_storeys, String urm_const_rem, String urm_cc_rem, String urm_comm_util, String urm_comm_util_other, String urm_pub_util, String urm_pub_util_other, String urm_ind_util1, String urm_plot_lease_ownby) in e:\LandMangement\App_Code\BOL\ResiMaster.cs:128
Residential.SaveResidentialbttn_Click(Object sender, EventArgs e) in e:\LandMangement\Residential.aspx.cs:104
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

protected void SaveResidentialbttn_Click(object sender, EventArgs e) 
    { 
 
        int retVal = 0; 
        //Insert 
        if (SaveResidentialbttn.Text == "Save"
        { 
            String AllotDate = Request.Form["txtAllotDate"]; 
            retVal = ResiMaster.Insert(ddlSector.SelectedValue, txtBlock.Text, txtPlot.Text, DateTime.Parse(txtEntryDate.Text), 
                                         txtLocation.Text, txtCatagory.Text, Int32.Parse(txtGroundRent.Text), txtPerPeriod.Text, 
                                         txtGroundRentDesc.Text, ddlResi.SelectedValue, ddlShopAllowed.SelectedValue, ddlIfResdenditial.SelectedValue, 
                                         ddlIfNotResidential.SelectedValue, txtAllotmentNo.Text, Convert.ToDateTime(AllotDate), ddlPreviouslyAlloted.SelectedValue, 
                                         txtFavour.Text, DateTime.Parse(txtDateOfExecution.Text), DateTime.Parse(txtDateOfRegistration.Text), txtCoOptHousingSociaty.Text, 
                                         txtSociatyreg.Text, Convert.ToInt32(txtSalami.Text), Int32.Parse(txtInterestPaid.Text), Int32.Parse(txtBPValue.Text), 
                                         Int32.Parse(txtTotalValue.Text), Int32.Parse(txtBalenceDue.Text), DateTime.Parse(txtDateOfpossession.Text), txtCertificateNo.Text, 
                                         txtTypeOfPossession.Text, Int32.Parse(txtGroundRentPaidUpto.Text), Int32.Parse(txtGroundRentAmount.Text), Int32.Parse(TxtBalenceDueifAny.Text), 
                                         txtPlanSubmitted.Text, DateTime.Parse(txtOriginalDate.Text), txtNo.Text, txtRemarks.Text, 
                                         txtPlanSanctioned.Text, DateTime.Parse(txtDOSanctioned.Text), txtDOSanctionedNo.Text, DateTime.Parse(txtDORevisedSanction.Text), 
                                         txtDORevisedSanctionNo.Text, txtRemarksDORevised.Text, ddlTenYearsExpired.SelectedValue, DateTime.Parse(txtOriginalDate1.Text), 
                                         txttxtOriginalDate1.Text, Convert.ToDateTime(txtReviseddate1.Text), txtNoReviseddate1.Text, txtREmarksReviseddate1.Text, 
                                         txtStoreysPlanned.Text, urm_const_status.Text, txtNoofStorageComplete.Text, txtConstructionStroge.Text, 
                                         txtConstructionRemarks.Text, txtCGGrantedFloorwise.Text, ddlCommercialUtilisationDetails.SelectedValue, txtOtherUtilisation.Text, 
                                         ddlPublicUtilisationDetails.SelectedValue, txtOtherUtilisation1.Text, txtIndustrialutilisation.Text, ddlPlotLeaseOwnedBy.SelectedValue); 
 
            ResTableDataBind(); 
            ImageOk.Visible = true
            Msg.Text = "New Record Insert Sucessfully"
            ClearMain(); 

//Insert Operation 
        public static int Insert( String sector,String block,String plot,DateTime urm_entry_dt, 
                                  String urm_loc_adv,String urm_alott_catg,Int32 urm_grnd_rent,String urm_grnd_rent_pp, 
                                  String urm_grnd_rent_desc,String urm_resi,String urm_shop_allowed,String urm_yes_resi, 
                                  String urm_non_resi,String urm_allot_no,DateTime urm_allot_dt,String urm_prev_alloted, 
                                  String urm_whose_fav,DateTime urm_reg_dt,DateTime urm_exe_dt,String urm_coop_name, 
                                  String urm_coop_reg_no,Int32 urm_org_cost,Int32 urm_interest_paid,Int32 urm_bp_cost, 
                                  Int32 urm_amount_paid,Int32 urm_bal_due,DateTime urm_poss_dt, String urm_poss_cert_no, 
                                  String urm_poss_type,Int32 urm_grnd_rentupto_yr,Int32 urm_grnd_rent_paid,Int32 urm_grnd_rent_bal, 
                                  String urm_pln_submitted,DateTime urm_pln_subm_org_dt,String urm_pln_subm_org_no,String urm_pln_subm_rem  , 
                                  String urm_pln_sanctioned,DateTime urm_pln_sanc_org_dt,String urm_pln_sanc_no, DateTime urm_pln_sanc_rev_dt, 
                                  String urm_pln_sanc_rev_no,String urm_pln_sanc_rem, String urm_tenyr_exp,DateTime urm_tenyr_org_dt, 
                                  String urm_tenyr_org_no,DateTime urm_tenyr_rev_dt, String urm_tenyr_rev_no, String urm_tenyr_rem,  
                                  String urm_pln_storeys,String urm_const_status,String urm_comp_storeys, String urm_const_storeys  ,  
                                  String urm_const_rem,String urm_cc_rem,String urm_comm_util,String urm_comm_util_other  ,  
                                  String urm_pub_util,String urm_pub_util_other,String urm_ind_util1,String urm_plot_lease_ownby) 
        { 
            DBAccess db = new DBAccess(); 
 
            SqlParameter objParam = new SqlParameter("@urm_id", 0); 
            objParam.Direction = ParameterDirection.Output;             
             
            db.Parameters.Add(new SqlParameter("@upm_sector", sector)); 
            db.Parameters.Add(new SqlParameter("@upm_block", block)); 
            db.Parameters.Add(new SqlParameter("@upm_plot", plot)); 
            db.Parameters.Add(new SqlParameter("@urm_entry_dt", urm_entry_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_loc_adv", urm_loc_adv)); 
            db.Parameters.Add(new SqlParameter("@urm_alott_catg", urm_alott_catg)); 
            db.Parameters.Add(new SqlParameter("@urm_grnd_rent", urm_grnd_rent)); 
            db.Parameters.Add(new SqlParameter("@urm_grnd_rent_pp", urm_grnd_rent_pp)); 
            db.Parameters.Add(new SqlParameter("@urm_grnd_rent_desc", urm_grnd_rent_desc)); 
            db.Parameters.Add(new SqlParameter("@urm_resi", urm_resi)); 
            db.Parameters.Add(new SqlParameter("@urm_shop_allowed", urm_shop_allowed)); 
            db.Parameters.Add(new SqlParameter("@urm_non_resi", urm_non_resi)); 
            db.Parameters.Add(new SqlParameter("@urm_yes_resi", urm_yes_resi)); 
            db.Parameters.Add(new SqlParameter("@urm_allot_no", urm_allot_no)); 
            db.Parameters.Add(new SqlParameter("@urm_allot_dt", urm_allot_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_reg_dt", urm_reg_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_exe_dt", urm_exe_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_coop_name", urm_coop_name)); 
            db.Parameters.Add(new SqlParameter("@urm_coop_reg_no", urm_coop_reg_no)); 
            db.Parameters.Add(new SqlParameter("@urm_org_cost", urm_org_cost)); 
            db.Parameters.Add(new SqlParameter("@urm_bp_cost", urm_bp_cost)); 
            db.Parameters.Add(new SqlParameter("@urm_interest_paid", urm_interest_paid)); 
            db.Parameters.Add(new SqlParameter("@urm_amount_paid", urm_amount_paid)); 
            db.Parameters.Add(new SqlParameter("@urm_bal_due", urm_bal_due)); 
            db.Parameters.Add(new SqlParameter("@urm_poss_dt", urm_poss_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_poss_cert_no", urm_poss_cert_no)); 
            db.Parameters.Add(new SqlParameter("@urm_poss_type", urm_poss_type)); 
            db.Parameters.Add(new SqlParameter("@urm_grnd_rentupto_yr", urm_grnd_rentupto_yr)); 
            db.Parameters.Add(new SqlParameter("@urm_grnd_rent_paid", urm_grnd_rent_paid)); 
            db.Parameters.Add(new SqlParameter("@urm_grnd_rent_bal", urm_grnd_rent_bal)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_submitted", urm_pln_submitted)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_subm_org_dt", urm_pln_subm_org_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_subm_org_no", urm_pln_subm_org_no)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_subm_rem", urm_pln_subm_rem));             
            db.Parameters.Add(new SqlParameter("@urm_pln_sanctioned", urm_pln_sanctioned)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_sanc_org_dt", urm_pln_sanc_org_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_sanc_no", urm_pln_sanc_no)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_sanc_rem", urm_pln_sanc_rem)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_sanc_rev_dt", urm_pln_sanc_rev_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_sanc_rev_no", urm_pln_sanc_rev_no)); 
            db.Parameters.Add(new SqlParameter("@urm_const_status", urm_const_status)); 
            db.Parameters.Add(new SqlParameter("@urm_const_storeys", urm_const_storeys)); 
            db.Parameters.Add(new SqlParameter("@urm_pln_storeys", urm_pln_storeys)); 
            db.Parameters.Add(new SqlParameter("@urm_comp_storeys", urm_comp_storeys)); 
            db.Parameters.Add(new SqlParameter("@urm_const_rem", urm_const_rem)); 
            db.Parameters.Add(new SqlParameter("@urm_comm_util", urm_comm_util)); 
            db.Parameters.Add(new SqlParameter("@urm_comm_util_other", urm_comm_util_other)); 
            db.Parameters.Add(new SqlParameter("@urm_pub_util", urm_pub_util)); 
            db.Parameters.Add(new SqlParameter("@urm_pub_util_other", urm_pub_util_other)); 
            db.Parameters.Add(new SqlParameter("@urm_ind_util1", urm_ind_util1)); 
            db.Parameters.Add(new SqlParameter("@urm_plot_lease_ownby", urm_plot_lease_ownby)); 
            db.Parameters.Add(new SqlParameter("@urm_prev_alloted", urm_prev_alloted)); 
            db.Parameters.Add(new SqlParameter("@urm_whose_fav", urm_whose_fav));             
            db.Parameters.Add(new SqlParameter("@urm_tenyr_exp", urm_tenyr_exp)); 
            db.Parameters.Add(new SqlParameter("@urm_tenyr_org_dt", urm_tenyr_org_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_tenyr_org_no", urm_tenyr_org_no)); 
            db.Parameters.Add(new SqlParameter("@urm_tenyr_rev_dt", urm_tenyr_rev_dt)); 
            db.Parameters.Add(new SqlParameter("@urm_tenyr_rev_no", urm_tenyr_rev_no)); 
            db.Parameters.Add(new SqlParameter("@urm_tenyr_rem", urm_tenyr_rem)); 
            db.Parameters.Add(new SqlParameter("@urm_cc_rem", urm_cc_rem)); 
 
            db.Parameters.Add(objParam); 
 
            int retval = db.ExecuteNonQuery("InsertResMasterNEW"); 
            if (retval == 1) 
            { 
                return int.Parse(objParam.Value.ToString()); 
            } 
            else 
            { 
                return -1; 
            } 
        } 


Please help

Thanks

Modify Computed Columns Using T-SQL

$
0
0
Hi,
I have a table with 4 columns let us say A,B,C,D.
column D is computed column with formula A + '-' + B
Now, i want to add one more condition to the formula which looks like "
A + '-' + B + '-' + C".

Please let me know how to do this using T-SQL as i cannot open the table in design mode in production server.

Thanks in Advance!!

Swapping the first and last characters in a column

$
0
0

Hi,

Fairly new to T-SQL and i want to swap the first and last characters in a column.

Thanks


Umar Javed

Help on Updating Column

$
0
0

I have a table with below columns and data

Subject                                                              Description

DC #1 - Emma Andre                                           qwertyuiopasdfghjklzxcvbnm

MD#1 - Jason Matt                                             helloworldhi

MM #2 - itsNEWYORK                                           NULL

DD #1 - Emma Matt                                           Emma Matt

I want to update subject like  below ...take first 25 characters from Description and replace on Subject after '-'

if it is NULL , take out the charters after '-' from Subject column 

Subject                                                              Description

DC #1 -qwertyuiopasdfghjklzxcvbn                    qwertyuiopasdfghjklzxcvbnmahjshJHl(34 CHARACTERS)

MD#1 - helloworldhi                                             helloworldhi

MM #2                                                                 NULL

DD #1 - Emma Matt                                           Emma Matt

Please help me !

Thannks

An update statement to use cumulative columns from the previous record

$
0
0

I need to create a update statement which updates a record based on a column from previous record and its column where they are grouped by another columns and ordered by date in ASC. Note that I need answer from SQL 2005+.

Suppose I have records:

TransactionID  ProductID    TransactionDate    Quantity        QuantityOnHand

1                          1                1/2/2014              2                     ?  2

2                          1                1/3/2014            3                     ? 5 = 3+2

3                          1                1/4/2014            1                     ? 6 = 5 + 1

4                          1                1/5/2014         9                     ? 15 = 6 + 9

I wrote this statement but did not work:

UPDATE it2 SET it2.QuantityOnHand =it2.Quantity + ISNULL(it1.QuantityOnHand,0)
FROM IT it1
LEFT JOIN (SELECT TransactionID,ProductID, Quantity,QuantityOnHand FROM IT GROUP BY ProductID ORDER BY TransactionDate)  it2 ON It2.TransactionID>it1.TransactionID
WHERE It2.ProductID=it1.ProductID

This means update the record and get the QuantityOnHand from its previous record if any grouped by the ProductID that are ordered by TransactionDate.  Note that in this example there is only one product ID. Here the QuantityOnHand column should be updated and TransactionDate is in ordered!


Mike



DDL:

CREATE TABLE [dbo].[IT](
    [TransactionID] [int] NOT NULL PRIMARY KEY,
    [ProductID] [int] NULL,
    [TransactionDate] [datetime] NULL,
    [Quantity] [float] NULL,
    [QuantityOnHand] [float] NULL
) ON [PRIMARY]

DML:

INSERT INTO [dbo].[IT] VALUES (1 ,1,'1/2/2014',2,NULL)
INSERT INTO [dbo].[IT] VALUES (2,1,'1/3/2014',4,NULL)        
INSERT INTO [dbo].[IT] VALUES(3 ,1 ,'1/4/2014' ,2,NULL)
INSERT INTO [dbo].[IT] VALUES(4 ,1 ,'1/5/2014' ,9,NULL)

Bringing back specific rows.

$
0
0

Hi SQL Gurus,

The first 3 columns are nvarchar(500) and the last one is a column i generated with a LEN function so its an INT and can have NULLs or 0. Below is the sample.  Field2 will be grouped together and if any of its corresponding field3 values do not match the rest of the Field2 values i need that row back. Thank you for any help.

Results
Results
Field1Field2Field3Field4
2Heading10010Material                                                 NylNULL
5Heading10013Jkyng0
5Heading10013Jklty0
5Heading10013Jkrty0
4Heading10012LenTTNULL
4Heading10012LenEENULL




DATA SET: Field1 -3 are nvarchar(500) and Field is an INT

Field1 Field2 Field3 Field4
1Heading 10009Type                                                                        Prim 124
1Heading 10009Type                                                                        Acc 54
1Heading 10009Type                                                                        Act 64
1Heading 10009Type                                                                        Deb 157
2Heading 10010Material                                                  Talc 94
2Heading 10010Material                                                  Nyl NULL
2Heading 10010Material                                                  Cub 249
2Heading 10010Material                                                  Alm 220
2Heading 10010Material                                                  Zirc 288
2Heading 10010Material                                                  Cer 196
2Heading 10010Material                                                  Sil 112
3Heading 10011Cut                                                                Fin NULL
3Heading 10011Cut                                                                Vry NULL
3Heading 10011Cut                                                                Crs NULL
3Heading 10011Cut                                                                Extr NULL
3Heading 10011Cut                                                                Md NULL
4Heading 10012Len TT NULL
4Heading 10012Len EE NULL
4Heading 10012Len GG 12
5Heading 10013Jk yng 0
5Heading 10013Jk lty 0
5Heading 10013Jk rty 0
5Heading 10013Jk rtu 12

Edwin Lopera



Finidng the missing segments and return to the output

$
0
0

Hello All,

I am working on finding the missing segments which doesn't have a code value. I have 2 tables, Line and Code.

In the line table I have below columns

Line_ID                  BeginMeasure                   EndMeasure

1                                0                                      100

2                                0                                      200

3                                0                                      300

In the code table I may have multiple records for each line

Line_ID                       BeginMeasure                  EndMeasure                   code

1                                  12                                    30                                    3

1                                   40                                   50                                    4

2                                  0                                      150                                  6

3                                  30                                    300                                  8

Here I need to find out the segments which doesn't have a code value. For example, for Line_ID 1 in the code table we dont have a value for the code for 0 -12, 30 - 40 and 50 - 100 segments and for Line_ID 2, we dont have code for 150 - 200 segment. I want the  records which doesn't have code value defined in the output result set and the expected result set should be like below

Line_ID                           BeginMeasure                     EndMeasure

1                                        0                                            12

1                                        30                                          40

1                                        50                                           100

2                                       150                                          200

3                                         0                                             30

Any code suggestions/help is greatly appreciated.

Thanks so much


Is there any other options to CHECKSUM?

$
0
0

For a data cleansing process I'm working on, I'm creating a new key, based on a combination of name + address.

I wanted to use something in TSQL that will allow me to create an unique key based on an string. 

What's the most modern solution for the question above in SQL 2012?

ObjectID from PageID

$
0
0

Is there any way to obtain the objectID from a DatabaseID & PageID WITHOUT using DBCC Page.

I'm returning Data from the Blocked Process Report and want to show which resource is being blocked.

For certain locks means i need to find the ObjectID from the PageID.

Ideally I would like to do this using a function or view in order to access it within the query.

And of course DBCC Page cannot be used in a view or Function.

edited to add: SQL Server 2008 R2 :-)

Thanks in advance

Paul


WHERE Clause, IN, Input Parameter

$
0
0

Hi everybody

I have a Stored Procedure with one input parameter called @GL_Code.

Somewhere near the end, I use a WHERE clause to filter my data, depending on the value of @GL_Code input by the user:

--------------------------------------------------------------

WHERE T1.Account = 

CASE

WHEN @GL_Code = 'A'     THEN 1240601

WHEN @GL_Code = 'B'     THEN 1240602

WHEN @GL_Code = 'C'     THEN 1240603

/*     WHEN @GL_Code = 'ALL' THEN ... ? ? ? ?   */

END

-----------------------------------------------------------

I want to add the foll. condition to the CASE:

If the user inputs 'ALL' as @GL_Code, the query should return all the following T1.Account: 1240601, 1240602, 1240603.

How can I do this?

Thanks

Leon Lai

Query performance tuning

$
0
0

Hello,

Can someone please tell me if there is anyway to improve this query to perform better.I tried using temp table instead of CTE .it did'nt change anything.I think there is a problem with latter part of query.When I execute every select individually they run faster.This query is taking hours to execute and is taking all the CPU .

lucky


lucky




How can I display previous monthname including the year?

$
0
0

I have the following sql statement:

SELECT Cast(datename(month, dateadd(mm, -1, getdate())) as varchar(15)) + '-' + Cast((DATEPART(YYYY,GETDATE())) As CHAR(4))

The result displays as July-2014 which is correct for previous month...

But on next year January-2015 it will display December-2015.

Is it possible to have the date shown as December-2014?

I appreciate any help.

Thanks in advance.

Viewing all 12890 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>