Dynamically Decide The Conditions In The 'WHERE' Clause
Apr 1, 2008
I have a report and in it there is a dataset that of course contains a query.
I want the query conditions to be changed automatically (the 'WHERE' clause) according to the environment it runs on, so if I put the same report on different customer computers, it will act differently according to the relevant 'WHERE' clause conditions.
Is it possible to use a parameter or "solution configurations" (or something else) in order to decide the conditions in the 'WHERE' clause?
Help will be really appreciated.
Thanks in advance.
View 1 Replies
ADVERTISEMENT
Sep 23, 2006
Is there a way I can build a case statment or similar to handle different where conditions?
I know I can do it dynamic sql but it would be nice to have a method where I can create the querries directly in a normal statement
Someting like:
Select c1, c2, c3
From Table
WHERE Case @Condition
WHEN '>' @SelColumn > @Limit
WHEN '=' @SelColumn = @Limit
WHEN '<' @SelColumn < @Limit
END
I know this doesn't work so an example that do work would be nice.
View 8 Replies
View Related
Mar 19, 2012
Code:
Drop table #table
Drop table #table_with_groupid
-- Prepare test data
CREATE TABLE #table
([Admissions_key] bigint NOT NULL PRIMARY KEY,
MRN nvarchar(10) NOT NULL,
[Code] ....
How can I compare dates with conditions. I only want to Mark C where the difference between adm_datetime and prevsep_datetime is <= 1 otherwise E as well
where datediff(MINUTE,tg.adm_datetime,tg.pre_sep_date)< =1 ??
is it correct ? where should I put this to implement correctly ?
View 1 Replies
View Related
Feb 12, 2008
Hi Faculties,I have two queries which give me the same output.-- Query 1SELECT prod.name, cat.nameFROM products prod INNER JOIN categories catON prod.category_id = cat.idWHERE cat.id = 1;-- Query 2SELECT prod.name, cat.nameFROM products prod INNER JOIN categories catON prod.category_id = cat.id AND cat.id = 1;The first query uses the WHERE clause and the second one has all theconditions in the ON clause. Is there anthing wrong with the secondapproach in terms of performance? Please suggest.Thanks in advanceJackal
View 6 Replies
View Related
Dec 12, 2007
I have a table (GLTRANS) with thousands of lines.
1 column in the table (ACCNO) has 300 different values which all need to change to a new value.
ie. 11100 all change to 8100
11200 all change to 8200
I know how to do a simple UPDATE
UPDATE GLTRANS
SET ACCNO = '8100'
WHERE ACCNO = '11100'
But how can i combine into 1 script rather than having to continually change this script 300 times??
Thanks
Wilbur
View 6 Replies
View Related
Mar 29, 2004
I look trough the forum, but did not find any simular problem. Somebody, help, please!
What I need to do is to write an algorithm which create a FROM clause for SQL query, using tables and joined fields, specified by the user. There could be up to 25 tables with any type of join (INNER, OUTER, FULL, CROSS). I know the basic structure of the FROM clause: "from T1 inner(or other type) join T2 on T1.field=T2.field" etc., but the main problem that users can specify tables in any order and I have to re-arrange them to create valid statement.
View 10 Replies
View Related
Jul 8, 2014
What I need?: A way of dynamically inserting a list in the WHERE clause of a query. (in the form, WHERE ID = 1,2,3,6,9 etc)
Imagine an example DB with 3 columns Student ID, Name, Teacher_ID. (Lets assume Teacher_ID with value 100 means its the Headmaster)
I need to create a list with Student ID's, who are directly/indirectly under the Headmaster. Example:
Headmaster
Teacher 1 (ID 200)
Teacher 2 (ID 250)
Student 1 (ID 300)
Director
Teacher 4
Student 5
In the above example, since I only want those students/teachers under the headmaster, either directly/indirectly, my list would contain Teacher 1, Teacher 2, and Student 1. (In my case, just their ID's, so 200, 250, 300)
Director, Teacher 4 and Teacher 5 wouldnt be in the list since theyre not directly/indirectly Headmaster.
View 2 Replies
View Related
May 9, 2007
Hi,
I currently have a list of User IDs (in a flat file) and I need to connect to a database I have read-only access to, so that I can retrieve additional data about these users.
I imagined a package that ran a query something like:
SELECT * FROM table WHERE UserID IN (<dynamically populated from flat file>).
Can somebody give me some advice as to how I can achieve this (either the way I suggested or another way).
Kind Regards,
Adam.
View 11 Replies
View Related
Feb 8, 2008
Hello All,
I have created SP in SQL 2K5 and make the where clause as parameter in the Sp. i am passing the where clause from my UI(ie ASP.NET), when i pass the where clause to SP i am not able to fetch the results as per the given criteria.
WhereClause from UI: whereClause="where DefectImpact='High'"
SQL Query in SP: SELECT @sql='select * from tablename'
Exec(@sql + @whereClause )
Here i am not able to get the results based on the search criteria. Instead i am getting all the results.
Please help me in this regard.
Thanks,
Subba Rao.
View 11 Replies
View Related
May 27, 2015
I want to change Set clause of Update Statement dynamically based on some condition.
Basically i have 2 Update statments having same FROM clause and same JOIN clause.
Only diff is SET clause and 1 Where condition.
So i am trying to combine 2 Update statements into 1 and trying to avoid visit to same table twice.
Update t
Set CASE **WHEN Isnull(td.IsPosted, 0) = 0
THEN t.AODYD = td.ODYD**
*ELSE t.DAODYD = td.ODYD*
END
From #ReportData As t
Join @CIR AS tmp On t.RowId = tmp.Max_RowId
[Code] ....
But CASE statement is not working...
View 7 Replies
View Related
Jan 10, 2008
My question is fairly simple. When I join between two tables, I always use the ON syntax. For example:
SELECT
*
FROM
Users
JOIN UserRoles
ON (Users.UserRoleId = UserRoles.UserRoleId)
No problems there. However, if I then decide to further filter the selection based on some trait of the UserRole, I have two options: I can add the condition as a WHERE statement, or I can add the condition within the ON block.
--Version 1:
SELECT
*
FROM
Users
JOIN UserRoles
ON (Users.UserRoleId = UserRoles.UserRoleId)
WHERE
UserRoles.Active = 'TRUE'
-- Version 2
SELECT
*
FROM
Users
JOIN UserRoles
ON (Users.UserRoleId = UserRoles.UserRoleId
AND UserRoles.Active = 'TRUE')
So, the question is, which is faster/better, if either? The Query Analyzer shows the two queries have the exact same execution plan, which makes sense, since they're both joining the same tables. However, I'm wondering if adding the condition in the ON statement results in fewer rows the JOIN statement initially needs to join up, thus reducing the overall initial size of the results table before the WHERE conditions are applied.
So is there a difference, performance wise? I imagine that if Users had a thousand records, and UserRoles had 10 records, then the JOIN would create a cartesian product of the two tables, resulting in 10,000 records in the table before the WHERE conditions are applied. However, if only three of the UserRoles is set to Active, would that mean that the resulting table, before applying WHERE conditions, would only contain 3000 records?
Thanks for whatever information you can provide.
View 7 Replies
View Related
Apr 14, 2008
For example: we have 2 set {1,4,8}, {1,2,3,4,5,8,9,0}
Is there any other fast way to decide {1,4,8} is in cluded in {1,2,3,4,5,8,9,0} ?
beside loop, or insert them into two table then check not exists?
View 8 Replies
View Related
May 31, 2007
Hello,
I have a situation here...
where we are inserting data of candidate...which includes fields...
1. First Name, Middle Name, Last Name ... (varchar(50))
2. Passport ID--- (no duplicates allowed but may be null )
3. Telephone number... ( may be null but no duplicates allowed )
4. Date of Birth --- ( mandatory )
5. Email id .......(no duplicates allowed but may be null )
The problem is that we already have about 1 lakh enteries in the database..
and it is expected to grow with time...
We need to define indexers such that it doesnot take time for searching or while inserting a new datafield into the database.
I hope that i am clear.
since, i suspect that that as it grows it will become more and more slow...!
This is database to store "resumes of different candidates".
So have decided...that
1. name 2. Passport no. 3. Email id: 4. Contact number
be the fields to prevent duplicacy of candidate data...
This involves checking for candidates existence every time anybody tries to make a new entry into database.
Please help to decide over the indexers...
Any good suggestions are welcomed...
Thanks for the reply...
With regards,
Girish R. Pawar
View 1 Replies
View Related
Dec 11, 2007
Hi-
I have a scenario where I need to decide between SSIS and Replication. Customer has 50 GB Database in Oracle. And they want around 150 tables from Oracle database to be copied into SQL Server 2005 database. And this has to be done every night at 10.00 pm to synchronize the data.
I want to know whether the following options are possible to do and which one will give me the best performance.
Option : 1
Use SSIS to get the data (150 tables) from Oracle and import into SQL Server
Option : 2
Use Replication to get the data from Oracle and import into SQL Server. Is that possible to Replicate the 150 tables alone (not the entire Database) from Oracle to SQL?
Option : 3
Use Replication to copy the entire 50 GB Database from Oracle and store into SQL Server.
On these three options which one will give me the best performance?
Thanks in advance.
View 4 Replies
View Related
Jun 29, 2006
Hi all,
I have a stored proc that uses xp_cmdshell to boot off a batch file on the NT side of the box (box OS is Windows 2000 Advanced Server).
Here is the pertinent code:/*----- Kick off the NT bat job to suck over the data through the web service pipe*/
SELECT @NTCommand = 'D:TradeAnalysisWondaDataStoreJobsPullFromWONDA _InstitutionalRankings.bat ' + CONVERT(varchar(10), @ReqDate, 101)
EXECUTE @e_error = master.dbo.xp_cmdshell @NTCommand
SELECT @m_error = CASE WHEN ISNULL(@e_error, 0) <> 0 THEN (@e_error + 50000) ELSE @@Error END
IF @m_error <> 0 GOTO ErrorHandlerThe trouble is that the batch file is failing (soft error, caught internally to the batch file, which then kills itself, screaming loudly all the way).
The batch file is using the following "voice" in which to scream in pain as it dies (a.k.a., using this code to terminate itself, which kills the cmd shell and returns 13 as an error code)REM WonDBService.exe says we failed
Date /T
Time /T
EXIT /B 13
Meanwhile, back at the ranch (errr...back in stored procedure), what is being returned is a ZERO (in the first code block, @e_error is being set to ZERO when the xp_cmdshell returns from the bat file.
So, now that I have ruled out the obvious *LOL* how can I get my xp_cmdshell to realize it has failed miserably at the one, tiny, simple, not-too-much-to-ask, job that it is designed to do?
View 2 Replies
View Related
Jun 22, 2006
I'm trying to join a table and based on the value of a given column I would join using the column in question, however if the column is NULL then I want to make the join without the column in the join. so I think I want to do something like this:
Case E.a
when NULL
then LEFT JOIN EPD ON EPD.b = D.b
AND EPD.SD = (SELECT MAX(E1.SD) FROM E1
WHERE E1.b = EPD.b AND E1.a = EPD.a AND E1.SD <= T.WD)
Else
LEFT JOIN EPD
ON EPD.a = D.a
and EPD.b = E.b
AND EPD.SD = (SELECT MAX(E1.SD) FROM E1
WHERE E1.a = EPD.a AND E1.b = EPD.b AND
E1.SD <= T.WD)
end
however T-Sql does not seem to like my Case statement mixed into my From/join clauses.
Thanks,
Mark E. Johnson
View 6 Replies
View Related
Aug 14, 2007
I need to show my boss that 2005 will on average be faster than 2000.
Are there any performance benchmark results available to show this?
Also I need similar benchmarks to show 64 bit will be faster than 32 bit SQL 2005.
Ian
View 6 Replies
View Related
Oct 19, 2007
I´m wondering how to solve the following scenario with SSIS
I have a CITY table and a STATE table, I have to load a file with the information regarding to the CITY:
the state table is like this:
StateCode(PK) stateLegalCode stateName
============= ============== =========
1 01 Florida
the city table is like this:
citycode(PK) cityLegalCode cityname StateCode(FK)
============ ============= ======== =============
1 1001 Quakertown 1
the file has the following information
cityLegalCode cityName
============= ========
01-1001 Quakertown
...
how can I load the file into CITY table:
1-) with the file's cityLegalCode I have to split the string and if the two initial digits are 01 the registry must have 1 in the StateCode(FK).
how can I do something like that using SSIS???
thanks
View 3 Replies
View Related
Jun 26, 2007
I have a parameter for each field that lets me know if a field can be sorted or not.
What I want to be able to do is turn on or off interactive sorting for that column based on that parameters value.
In the dialog you have a check box that says enable interactive sorting. When that is clicked it appears that you get the two arrows no matter what you put in the expression.
I have tried
=IIF(allowSorting,Fields!myColumn.Value,"")
and
=IIF(allowSorting,Fields!myColumn.Value,Nothing)
but both result in the arrows still being there, just the sort doesn't work.
Is it possible to put an expression on the sort arrows appearing at all?
View 3 Replies
View Related
May 8, 2015
The user wants to be able, using excel, to apply a filter to all measures in every measure group. I though that I can create a dimension with a single level with two members, let´s say "on" and "off" and depending on the selected member and using an IIF statement decide which formula applies to the calculated measures.
I have serious doubts about the performance and for this technique because I am thinking as a .Net developer and not as a cube developer. Maybe it is better to resolve it scoping the measures but I cannot figure it out.
View 3 Replies
View Related
Jul 15, 2015
I have received some reports and I have been asked to decide whether these reports can be developed as an operational report or Analytical Reports.
Basically I wanted to understand what points needs to be considered while deciding whether I should go for Analytical reporting (Cubes) or Operation Reporting.
View 2 Replies
View Related
Nov 4, 2015
I have a quite big SQL query which would be nice to be used using UNION betweern two Select and Where clauses. I noticed that if both Select clauses have Where part between UNION other is ignored. How can I prevent this?
I found a article in StackOverflow saying that if UNION has e.g. two Selects with Where conditions other one will not work. [URL] ....
I have installed SQL Server 2014 and I tried to use tricks mentioned in StackOverflow's article but couldn't succeeded.
Any example how to write two Selects with own Where clauses and those Selects are joined with UNION?
View 13 Replies
View Related
Mar 2, 2006
I am in the process of locking down the SQL Server in an environment that is considered to be in production (pilot stages) and there is no staging or test environment that mirrors it. I need assistance in determining the server and database roles to assign to existing logins, most of which currently have sa and dbowner rights. Because it is not a development environment, I need to be sure that downgrading the server and/or database level permissions will not break any functionality.
I'm starting with the logins that have the SA fixed server role. These logins need to be able to install applications that require the use of a backend database, which will be stored on SQL Server. In addition, through the installation process a new login/password for the newly created database(s) is normally created. For the existing logins with the SA fixed server role, will downgrading to the securityadmin and dbcreator roles be sufficient to facilitate those needs, or are those too much/ too little? And should any user account ever be granted the SA role? If so, what questions could I ask to determine this need?
Since these install process for these applications usually prompt to install using SA or local system account to authenticate to SQL to create the new database(s), that account should have securityadmin and dbcreator roles to create the database and its tables, as well as add a new login to that database.
Please address this question, keeping in mind that the logins will only be performing the described actions, installing apps using SQL Server as the backend database and adding a login to that database (which may or may not be done during the installation process).
Thank you,
nu_dba
View 1 Replies
View Related
Jul 23, 2005
Hi, can anyone shed some light on this issue?SELECT Status from lupStatuswith a normal query it returns the correct recordcountSELECT Status from lupStatus GROUP BY Statusbut with a GROUP By clause or DISTINCT clause it return the recordcount= -1
View 3 Replies
View Related
Oct 25, 2007
I am working with a vendor on upgrading their application from SQL2K to SQL2K5 and am running into the following.
When on SQL Server 2000 the following statement ran without issue:
UPDATE dbo.Track_ID
SET dbo.Track_ID.Processed = 4 --Regular 1 leg call thats been completed
WHERE Processed = 0 AND LegNum = 1
AND TrackID IN
(
SELECT TrackID
FROM dbo.Track_ID
GROUP BY TrackID
HAVING MAX(LegNum) = 1 AND
TrackID + 'x1' IN
(
SELECT
dbo.Track_ID.TrackID + 'x' + CONVERT(NVARCHAR(2), COUNT(dbo.Track_ID.TrackID))
FROM dbo.Track_ID INNER JOIN dbo.transactions
ON dbo.Track_ID.SM_ID = dbo.transactions.sm_session_id
GROUP BY dbo.Track_ID.TrackID
)
)
Once moved to SQL Server 2005 the statement would not return and showed SOS_SCHEDULER_YIELD to be the waittype when executed. This machine is SP1 and needs to be upgraded to SP2, something that is not going to happen near time.
I changed the SQL to the following, SQL Server now runs it in under a second, but now the app is not functioning correctly. Are the above and the following semantically the same?
UPDATE dbo.Track_ID
SET dbo.Track_ID.Processed = 4 --Regular 1 leg call thats been completed
WHERE Processed = 0 AND LegNum = 1
AND TrackID IN
(
SELECT TrackID
FROM dbo.Track_ID
WHERE TrackID + 'x1' IN
(
SELECT dbo.Track_ID.TrackID + 'x' + CONVERT(NVARCHAR(2), COUNT(dbo.Track_ID.TrackID))
FROM dbo.Track_ID INNER JOIN dbo.transactions
ON dbo.Track_ID.SM_ID = dbo.transactions.sm_session_id
GROUP BY dbo.Track_ID.TrackID
)
GROUP BY TrackID
HAVING MAX(LegNum) = 1
)
View 3 Replies
View Related
Aug 7, 2007
I have a table
CREATE TABLE [dbo].[CmnLanguage]( [Id] [char](2) NOT NULL CONSTRAINT PkCmnLanguage_Id PRIMARY KEY, [EnglishName] [varchar](26) NOT NULL, [NativeName] [nvarchar](26) NOT NULL, [DirectionType] [smallint] NOT NULL, [IsVisible] [bit] NOT NULL, [CreatedDateTime] [datetime] NOT NULL DEFAULT GETDATE(), [ModifiedDateTime] [datetime] NULL)
We will use these 3 queries
select * from CmnLanguage where IsVisible = 0select * from CmnLanguage where IsVisible = 1select * from CmnLanguage
I want to make a method which handles these queries.
But at the back end on Stored Procedures
We have to write 3 queries
Which I don't want to do.
I want to minimize the queries and conditions
and want to just write one for these 3
Can any one do it?
View 2 Replies
View Related
May 31, 2000
Folks,
I'm having some real problems using the OR condition in a very simple SQL statement and could use your help or insight on where the problem lies, or perhaps a workaround.
I have a large flat table in a SQL 7 database with 10 million + records called "HISTORY". I have not installed either service pack 1 or 2. I'm attempting to run a query that references the following four fields which are all non-clustered keys:
EQUIPMENT_NO TEXT 12
CHASSIS_IN TEXT 12
CHASSIS TEXT 12
SVC_DATE_TIME SMALLDATETIME
Here's the SQL statement:
SELECT * FROM HISTORY WHERE (HISTORY.EQUIPMENT_NO = 'XYZ123' OR HISTORY.CHASSIS = 'XYZ123' OR HISTORY.CHASSIS_IN = 'XYZ123') AND SVC_DATE_TIME >= '01/15/00 00:00:00 AM' AND SVC_DATE_TIME <= '02/28/00 23:59:59 PM'
ORDER BY EQUIPMENT_NO
This query takes 11 min. 5 sec. inder the Query Analyzer and ultimately returns the 31 desired records.
If you remove the SVC_DATE_TIME criteria, about 350 records are returned in a matter of seconds. I've also tried variations on the date syntax such as '01/15/00', etc. with no change in the amount of time to execute.
Other queries such as a simple AND condition combining EQUIPMENT_NO and SVC_DATE_TIME are snappy.
Are there known problems/bugs with "OR" conditions in queries that anyone is aware of, particularly with parentheses; am I composing this query incorrectly? Is there some alternate syntax that would work as expected? I can't see where the query shouldn't execute quickly as expected, particularly with all indexed fields involved. I'm stumped! Lend me your expertise. Thanks much.
Clark R. Farabaugh, Jr.
Financial Systems Analyst
VIT
Norfolk, VA
View 8 Replies
View Related
Nov 29, 2007
Hai frns small help needed.
I have a table called sample and i have the following requirement. i.e i need sum(credit) group by ssn no.
One special condition is as follows:
For each distinct ssn if "flag" has the same CX value,then out of all the records with the same CX value, the highest "credit" value is added to the sum for that "ssn" and the rest are ignored.
If while adding "credit" to the sum and if "credit" value is equal to zero then "sum" value is used for summing else "credit" value is used.
Can any one help me out in trying this logic. I have tried but i could'nt able embed the conditions inbetween the Sql statetment.
Here is the query is used
select * from sample
idssncreditflagsem
11010C90
21014C93
31014.5C92
41013.5C11
51024.2C33
61030C12
select ssn,flag,sum(case credit when 0 then sem else credit end) as sum from sam2
group by ssn,flag
ssn flag sum_val
101C13.5
103C12.0
102C34.2
101C98.5
The above output is wrong one.
Expected output
101 4.5+3.5=8.0
102 4.2
103 2.0
Any help would be appreciated
Regards,
View 5 Replies
View Related
Oct 14, 2014
I have the following:
MAX(Vernum) AS test,
MAX(Case WHEN A2.AttrID = 2 AND A2.DefID = 10057945 THEN A2.ValStr END) AS TYPE_DOCUMENT
This works not perfect. In many cases I have more then one row and my query takes the max value of column Valstr. Thats is not exactly what I want. I'd like to have the value of Column Valstr of the row where column Vernum has the maximum value.
I've tried many things like:
MAX(Vernum) AS test,
MAX(Case WHEN A2.AttrID = 2 AND A2.DefID = 10057945 AND A2.Vernum=test THEN A2.ValStr END) AS TYPE_DOCUMENT
OR
MAX(Vernum) AS test,
MAX(Case WHEN A2.AttrID = 2 AND A2.DefID = 10057945 AND A2.Vernum=MAX(A2.Vernum) THEN A2.ValStr END) AS TYPE_DOCUMENT
View 1 Replies
View Related
Sep 12, 2006
Hi All.
Is there a way to have multiple AND conditions on the same field in a database.
EXAMPLE
SELECT * FROM tbl
WHERE field1 = 1 AND field1 = 2 AND field1 = 5
Thanks
View 9 Replies
View Related
Oct 24, 2006
pls:
1/ can we do it this way:
inner join Table2 ON table1.fld1=table2.fld21 AND table1.fld12=table2.fld22
2/also:
what s the difference between join , iner join and left join
Thanks .
View 4 Replies
View Related
Jul 16, 2007
ALTER PROC usp_t_insup_cpa1
@Idint,
@SupervisorIdint,
@BookmarkerIdint,
@PreparerIdint,
@FirmNovarchar(20),
@FirmNamevarchar(30),
@FirstNamevarchar(20),
@MiddleNamevarchar(20),
@LastNamevarchar(20),
@TaxYearvarchar(20),
@TaxSoftwarevarchar(20),
@HomePhonevarchar(20),
@WorkPhonevarchar(20),
@Faxvarchar(20),
@PrimaryEmailvarchar(30),
@SecondaryEmailvarchar(30),
@CountryIdint,
@Statevarchar(20),
@Zipcodevarchar(20),
@Statusint,
@OperatorChar(1) = '',
@RESULTINT OUTPUT
-------------------------
AS
IF @Operator = 'I'
BEGIN
IF NOT EXISTS(SELECT * FROM CPA WHERE PrimaryEmail=@PrimaryEmail or PrimaryEmail=@SecondaryEmail or SecondaryEmail=@PrimaryEmail or SecondaryEmail=@SecondaryEmail )
BEGIN
--select * from o_login
Begin transaction InsCPA
INSERT INTO CPA(SupervisorId,BookmarkerId,PreparerId,FirmNo,FirmName,FirstName,MiddleName,LastName,TaxYear,TaxSoftware,HomePhone,WorkPhone,Fax,PrimaryEmail,SecondaryEmail,CountryId,State,Zipcode,Status)
VALUES(@SupervisorId,@BookmarkerId,@PreparerId,@FirmNo,@FirmName,@FirstName,@MiddleName,@LastName,@TaxYear,@TaxSoftware,@HomePhone,@WorkPhone,@Fax,@PrimaryEmail,@SecondaryEmail,@CountryId,@State,@Zipcode,@Status)
--Error handling
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN InsCPA
SET @RESULT = 0
END
ELSE
BEGIN
--DECLARE @ID1 INTEGER
-- Returns 1 to the calling program to indicate success.
COMMIT TRAN InsCPA
SET @RESULT = 1
END
END
ELSE
BEGIN
-- Return 2 to the calling program to indicate record already exists.
set @RESULT = 2
END
END
ELSE IF(@Operator='U')
BEGIN
declare @pemail as varchar(30)
declare @semail as varchar(30)
declare @firm as varchar(20)
select @pemail=PrimaryEmail,@semail=SecondaryEmail,@firm=FirmNo from CPA WHERE Id = @Id
--select * from CPA
if(@pemail=@PrimaryEmail) or(@semail=@PrimaryEmail)--or((@semail=@SecondaryEmail)and (@semail=@PrimaryEmail)))
begin
print 'prim1'
if(@semail=@SecondaryEmail)or (@pemail=@SecondaryEmail)
begin
print 'sec1'
if(@firm=@FirmNo)
begin
print'firm'
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0
END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1
END
END
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE FirmNo=@FirmNo)
BEGIN
print'fd'
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0
END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1
END
end
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'4'
--COMMIT TRAN UpdateCPA
SET @RESULT = 4
END
end
end
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE PrimaryEmail=@SecondaryEmail or SecondaryEmail=@SecondaryEmail)
BEGIN
if(@firm=@FirmNo)
begin
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0
END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1
END
END
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE FirmNo=@FirmNo)
BEGIN
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0
END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1
END
end
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'44'
--COMMIT TRAN UpdateCPA
SET @RESULT = 4
END
end
/*
--select * from o_login
Begin transaction InsCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
--Error handling
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN InsCPA
SET @RESULT = 0
END
ELSE
BEGIN
--DECLARE @ID1 INTEGER
-- Returns 1 to the calling program to indicate success.
print'11'
COMMIT TRAN InsCPA
SET @RESULT = 1
END*/
END
ELSE
BEGIN
print 'sec same'
-- Return 2 to the calling program to indicate record already exists.
set @RESULT = 3
END
end
end
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE PrimaryEmail=@PrimaryEmail or SecondaryEmail=@PrimaryEmail)
BEGIN
/*--select * from o_login
Begin transaction InsCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
--Error handling
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN InsCPA
SET @RESULT = 0
END
ELSE
BEGIN
--DECLARE @ID1 INTEGER
-- Returns 1 to the calling program to indicate success.
print'111'
COMMIT TRAN InsCPA
SET @RESULT = 1
END*/
if(@firm=@FirmNo)
begin
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0
END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1
END
END
else
begin
IF NOT EXISTS(SELECT * FROM CPA WHERE FirmNo=@FirmNo)
BEGIN
BEGIN TRANSACTION UpdateCPA
UPDATE CPA
SET SupervisorId=@SupervisorId,
BookmarkerId=@BookmarkerId,
PreparerId=@PreparerId,
FirmNo=@FirmNo,
FirmName=@FirmName,
FirstName=@FirstName,
MiddleName=@MiddleName,
LastName=@LastName,
TaxYear=@TaxYear,
TaxSoftware=@TaxSoftware,
HomePhone=@HomePhone,
WorkPhone=@WorkPhone,
Fax=@Fax,
PrimaryEmail=@PrimaryEmail,
SecondaryEmail=@SecondaryEmail,
CountryId=@CountryId,
State=@State,
Zipcode=@Zipcode,
Status=@Status
WHERE Id = @Id
UPDATE EMPLOYEE
SET FirmNo=@FirmNo
WHERE FirmNo=@firm
IF @@ERROR <> 0
BEGIN
-- Returns 0 to the calling program to indicate failure.
ROLLBACK TRAN UpdateCPA
SET @RESULT = 0
END
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'1'
COMMIT TRAN UpdateCPA
SET @RESULT = 1
END
end
ELSE
BEGIN
-- Returns 1 to the calling program to indicate success.
print'2'
--COMMIT TRAN UpdateCPA
SET @RESULT = 2
END
end
END
ELSE
BEGIN
print 'prim same'
-- Return 2 to the calling program to indicate record already exists.
set @RESULT = 2
END
end
end
Above procedure has many if else conditions
Is there any way to write
procs other than this process
Malathi Rao
View 1 Replies
View Related
Jul 26, 2007
Hi folks, basically I'm looking for this sort of structure
WHERE (caseA AND caseB) OR (caseC AND caseD) OR (CaseA AND caseD)
but I can't seem to be able to group the AND conditions together any time I try put brackets in SQL Server Enterprise manager removes them on me,
any help would be great,
thanks
View 1 Replies
View Related