I need some help writing my request, I tried both of the following but it does'st seem to work. Your sugestion would be appreciated.SELECT count(LeadId) FROM dbo.Cl_Leads Where AccntMng=@AccntMng and (Status = 'Won' or Status = 'Lost') and InsertDate BETWEEN @dtStart AND @dtEnd I also tried SELECT count(LeadId) FROM (SELECT * FROM dbo.Cl_Leads WHERE InsertDate BETWEEN @dtStart AND @dtEnd)Where AccntMng=@AccntMng and (Status = 'Won' or Status = 'Lost') This return and error: Incorrect syntax near the keyword 'Where'.
'****************************************************************** '****************************************************************** Public Function Add_Cl_Contact(ByVal strContactName As String, _ ByVal strDep As String, ByVal strEmail As String, ByVal strExt1 As String, ByVal strExt2 As String, _ ByVal strExt3 As String, ByVal strFax As String, ByVal strPhone1 As String, ByVal strPhone2 As String, _ ByVal strPhone3 As String, ByVal strTitle As String, ByVal iContactTypeId As Integer, _ ByRef iResult As Integer, ByRef strError As String) As Boolean '****************************************************************** Dim bSuccess As Boolean = True Dim connect As New SqlConnection(strConnection) Dim cmdSelect As New SqlCommand("Add_Cl_Contact", connect) Dim paramReturnValue As SqlParameter
If cmdSelect.Parameters("ReturnValue").Value = 0 Then strError = "Contact has been added" Else strError = strContactName & " already exist!" bSuccess = False End If
Catch ex As Exception bSuccess = False strError = ex.Message Finally If connect.State = ConnectionState.Open Then connect.Close() End If End Try Return bSuccess End Function
For a reason that I dont see my store proc is always returning 0 records but if I use a commandType.text instead of StoredProcedure then I get my results. So I must have some kind of error somewhere. Store Proc ALTER PROCEDURE dbo.Get_Cl_IssuesBySystemID
LEFT OUTER JOIN dbo.IssueTracker_ProjectCustomFieldValues t2
ON t1.IssueId = t2.IssueId
Where t2.CustomFieldId=5 and t2.CustomFieldValue like '%@SystemId%'
order by t1.DateCreated
SET NOCOUNT OFF
Function Public Function Get_Cl_IssuesBySystemID(ByVal strSystemId As String) As DataView Dim objDs As New DataSet Dim objDv As New DataView Dim connect As New SqlConnection(strConnection)
'***For CommanType.Text*** Dim strSelect As String
" LEFT OUTER JOIN dbo.IssueTracker_ProjectCustomFieldValues t2" & _ " ON t1.IssueId = t2.IssueId" & _
" Where t2.CustomFieldId=5 and t2.CustomFieldValue like '%" & strSystemId & "%'" & _ " order by t1.DateCreated" Dim cmdSelect As New SqlCommand(strSelect, connect) cmdSelect.CommandType = CommandType.Text '***End For CommanType.Text***
'***For CommanType.StoredProcedure*** 'Dim cmdSelect As New SqlCommand("Get_Cl_IssuesBySystemID", connect) 'cmdSelect.CommandType = CommandType.StoredProcedure 'PARAM 'cmdSelect.Parameters.Add("@SystemId", strSystemId) '***End For CommanType.StoredProcedure***
Can someone help me with debugging store proc. I am usig VS 2003 and SQL Server 2005 and I have no clue as how to do that if it is possible. I would like to place a break point in my store proc if that is possible. I know if I was in VS 2005 it would be a charm but it's not my case. Thanks
I am trying to do an insert with a SPand I get the following error: "The name 'A2LA Website' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted." When iParentID=30 and strTexte="A2LA Website"
What an I doing wrong?
Table: ID int(identity) ParentID int Text nvarchar(50) Valeur nvarchar(50) Allow Null Ordre int Allow Nulls
Public Sub addItemToDdSelection(ByVal strTable As String, ByVal iParentID As Int16, ByVal strTexte As String) Dim connect As New SqlConnection(strConnection) Dim cmdSelect As New SqlCommand("AddDropDownContent", connect) Dim paramReturnValue As SqlParameter Dim strError As String
cmdSelect.CommandType = CommandType.StoredProcedure 'PARAM cmdSelect.Parameters.Add("@intParentID", iParentID) cmdSelect.Parameters.Add("@strTexte", strTexte) Try connect.Open() cmdSelect.ExecuteNonQuery() connect.Close() Catch ex As Exception
strError = ex.Message Finally If connect.State = ConnectionState.Open Then connect.Close() End If End Try End Sub
ALTER PROCEDURE dbo.AddDropDownContent ( @intParentID int, @strTexte varchar(50) ) AS EXEC ('INSERT INTO DropDownMenus (ParentID, Texte) VALUES(' + @intParentID + ',"' + @strTexte + '")')
I have the following store proc and was wondering if I can optimized it by using a SELECT CASE instead of all those IF? I tried but don't know how to write it. Thanks set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go
ALTER PROCEDURE [dbo].[Get_Cl_SearchMultiColumn] ( @strSearchTermColumnNamenvarchar (50), @strSearchTermSearchTermnvarchar (200) )
as
if (@strSearchTermColumnName = 'Monitor') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Monitor1,Monitor2 FROM Cl_Systems WHERE contains(Monitor1,@strSearchTerm) or contains(Monitor2,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'MonitorSerial') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Monitor1Serial,Monitor2Serial FROM Cl_Systems WHERE contains(Monitor1Serial,@strSearchTerm) or contains(Monitor2Serial,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'Microscope') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Microscope1,Microscope2 FROM Cl_Systems WHERE contains(Microscope1,@strSearchTerm) or contains(Microscope2,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'SerialMicroscope') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,SerialMicroscope1,SerialMicroscope2 FROM Cl_Systems WHERE contains(SerialMicroscope1,@strSearchTerm) or contains(SerialMicroscope2,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'Controller') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Controller1,Controller2 FROM Cl_Systems WHERE contains(Controller1,@strSearchTerm) or contains(Controller2,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'ControllerFirmware') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Cont1Firmware,Cont2Firmware FROM Cl_Systems WHERE contains(Cont1Firmware,@strSearchTerm) or contains(Cont2Firmware,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'SerialController') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,SerialController1,SerialController2 FROM Cl_Systems WHERE contains(SerialController1,@strSearchTerm) or contains(SerialController2,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'Joystick') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Joystick1,Joystick2 FROM Cl_Systems WHERE contains(Joystick1,@strSearchTerm) or contains(Joystick2,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'JoystickFirmware') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Joy1Firmware,Joy2Firmware FROM Cl_Systems WHERE contains(Joy1Firmware,@strSearchTerm) or contains(Joy2Firmware,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'SerialJoystick') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,SerialJoystick1,SerialJoystick2 FROM Cl_Systems WHERE contains(SerialJoystick1,@strSearchTerm) or contains(SerialJoystick2,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'Camera') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Camera1,Camera2,Camera3,Camera4 FROM Cl_Systems WHERE contains(Camera1,@strSearchTerm) or contains(Camera2,@strSearchTerm) or contains(Camera3,@strSearchTerm) or contains(Camera4,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'CameraSerial') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Camera1Serial,Camera2Serial,Camera3Serial,Camera4Serial FROM Cl_Systems WHERE contains(Camera1Serial,@strSearchTerm) or contains(Camera2Serial,@strSearchTerm) or contains(Camera3Serial,@strSearchTerm) or contains(Camera4Serial,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'ZMotor') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,ZMotor1,ZMotor2,ZMotor3 FROM Cl_Systems WHERE contains(ZMotor1,@strSearchTerm) or contains(ZMotor2,@strSearchTerm) or contains(ZMotor3,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'Stage') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Stage1,Stage2,Stage3 FROM Cl_Systems WHERE contains(Stage1,@strSearchTerm) or contains(Stage2,@strSearchTerm) or contains(Stage3,@strSearchTerm) return 0 end
if (@strSearchTermColumnName = 'Lens') begin SELECT CustomerID,SystemId,CompanyName,City,State,Country,Lens1,Lens2,Lens3 FROM Cl_Systems WHERE contains(Lens1,@strSearchTerm) or contains(Lens2,@strSearchTerm) or contains(Lens3,@strSearchTerm) return 0 end
I am trying to code a store procedure using nested cursor and I don't know where I am doing wrong. I am getting message:
Server: Msg 170, Level 15, State 1, Procedure sp_alicare_99, Line 212 Line 212: Incorrect syntax near 'first_cursor'.
Can someone please help me with this, it's very urgent and I have to use it for production purposes.
Here is the store proc:
-- 05/24/2000 V1.0 -- populate Los Table
print 'sp_alicare_99' go if exists(select name from sysobjects where name='sp_alicare_99' and type='P') drop procedure sp_alicare_99 go CREATE PROC sp_alicare_99 As Begin /********** * sp_alicare_99 (V603.132) ***********/
Hey, How do I put anything there for an INT type??? It is in a stored procedure and I have six parameters that I am passing in, some are blank and I want to put a default all variable in the where statement.
Select tbl_EventDate.EventDate , tbl_EventDate.EventDateID_p FROM tbl_EventDate WHERE EventDateID_p = %
This does not work.... I tried * too..... hmmmm....
@usrcode as varchar(5) = '%', @disch_dt_start as varchar (12) = '%', @disch_dt_end as varchar (12) = '%', @client_id char(2) = '%' as
select distinct t.patient_id,p.first_name,p.last_name,convert(char (12),p.birthdate,101) Birthdate,sex,t.auth_id,t.place_of_service,convert (char(12),t.discharge_date,101) Discharge_date, i.service_id from patient_transaction t inner join inpatient_service i on t.patient_id = i.patient_id and t.tran_id = i.tran_id and CONVERT(VARCHAR,ISNULL(i.service_id ,0)) LIKE @usrcode and t. place_of_service = '1' and t.decision in ('1', '2') and t.discharge_date between convert(char(12),@disch_dt_start,101) and convert(char(12),@disch_dt_end,101) and CONVERT(VARCHAR,ISNULL(substring(t.patient_id,1,2) ,0)) LIKE @client_id inner join patient p on t.patient_id = p.patient_id order by 1
I want to get all clients if I don't specify the parameter @client_id but when I do that I get 0 records. What am I doing wrong??
i m new in the world of database so please help me to create a perticular store proc
i want to get data from 3 tables and 2 field of a table is match with the same field of another table and it cant work i write that code so u can understand easily but it dont work so please modify that and make it workable
select a.a_dis_id,b.name as fromname,b.name as toname,a.a_dis_km,c.a_all_name,c.a_all_rate from a_distance a,levels b,a_allowance c where a.a_dis_from=b.cid and a.a_dis_to=b.cid and c.a_all_id=a.a_all_id
---Master query (Assuming this will display 20 rows) we are dealing with one single table that we need to pivot. select id,fname,lname,sponsor from masterfile where id='TARZAN'
---from those 20 rows there is id that sponsored some one else ---explain: assuming ID=SHAGGY FNAME=Shaggy LNAME=Scooby (was sponsored by Tarzan) ---but Shaggy has sponsored 2 others select id,fname,lname,sponsor from masterfile where id='SHAGGY'
---will display 3 rows and if from one of those 3 others that belongs to shaggy ---I also want to get their information ID,fname,lname ---This can go up to 10 per saying is like building a Tree with branches and leaves under those branches
---Explain: ---Let's assume that we have an OAK Tree that has 4 main branches ---and out of those 4 main branches 2 of them have other branches with leaves under it
--I would like to do this process in a cursor (Store Proc) is possible --the way I have it now taking way too long --because in within so many (do while loop)
TIA Please pardon me, I could not find better layout to explain this.
I am looking for a store procedure which validates certain keywords like delete,truncate,update,insert etc. and restricts them to be used by users in all of my store procs which takes strings as inputs.
Is it possible that i can use a store proc to modify all the rest of my store procedures that i have in my DB ??
I have so many created allready and it will be to long to go throught each one of them to modify my text inside.
what i wish to do is a store proc that will allow me to loop to all my store proc of the current DB and look inside for specific text that i would like to change with the new value !!
Hello i have this store proc with the syntax below. The getdate get the current date but i need to change the date this one time to 3/20/2005. I was wondering is there a way to do that an not modify my sp. I tried to harcode 3/20/2005 as asofdate and i get all 0 in my table.
One of the Sql server crashed and when the server restart, the Store Procs have locked icon. Can't modify or do anything with it, but the proc still run. What could have cause this, and how can I remove the lock icon?
I am new to sql server 2005 but this should be easy but what ever. Could someone explain how I can edit my existing store procedure from Management Studio? Any time I do a save it wants to save a .sql file ! Thanks
Please stick with me for a second... If someone dealt with tables related to US State/County and ZIP. We found a company on the Web which have ALL US counties, states, zips and cities in one big table. We need somehow to break this table( about 76K records) into multiple related tables and estabsish Some sort of relationship among these tables. 1.State table(design) ID State_Name 2.County table( design) ID County_Name Relationship_to_State For example if state = 'NY'; then all Counties falls in NY should have relationship '01'. For NJ '02' and so on. 3.Zipcode table( design-same as for county)
If someone has any idea how to solve this problem that would be really appreciated. Thanks a lot in advance.
I am using below code to get patient status as an out put to my form. not sure whats happening but each time I run this its not executing my last "IF" if set to "N" show me N if not show me "Y" but it is by passing my first "IF" condition and jumps to last?
The column alerts_ind shows only Y or N in the table patient_status.
The table patient_status_mstr show the description of the patient which "discharged". All I want to do is if the patient is flagged with "discharge" the columns "alerts_ind" shows "Y". but something wrong? below is the code.
Alter PROCEDURE GBCheckPatientStatus (@enc_id varchar(36), @data_ind Char(1) OUTPUT)
as begin declare @alerts_ind char(1); select @alerts_ind =pm.alerts_ind
For extracting the link structure of a dependency network with a large number of nodes (for problems having a large number of variables), we have been using the stored procedure:
I have stored procedure which runs on some period, what i want is when it first run i want to store that date permanently or till the next time it runs again, and then i need to take my data from that store proc , where hist_date between (date stored when it ran first time,example, 08/21/2015) and today date.(exampple, 08/24/2015)
Now next time when it runs , date stored should be updated in this case it should be (08/24/2015) .
How can i do this in stored proc, I tried to use temp table but it didn't work .
DECLARE @SdxLastName char(4) DECLARE @TheFirstName varchar(30) DECLARE @HitCountLastNameOnly int DECLARE @HitCountFirstName int DECLARE @ThresholdLastName int DECLARE @ThresholdFirstName int
-- Set minimum row hit count for when to use the first name in addition to last name for search SET @ThresholdLastName = 2 --if we get at least this number of rows back, ignore the first name completely -- Set maximum row hit count for when to use the exact first name instead of first name soundex in addition to last name soundex for search SET @ThresholdFirstName = 12 --if we get over this number of rows back, look for exact first name match
IF @AN_CustID < 1 OR @LastName = '' RETURN --required params
-- Whenever a doctor's first name is unknown, the user will enter "DR"; therefore, do not soundex the first name in this case IF @FirstName='DR.' OR @FirstName='DR' SET @FirstName=''
-- Get the soundex of the specified physician SELECT @SdxLastName = dbo.aif_Soundex(@LastName), @TheFirstName = dbo.aif_Soundex(@FirstName)
-- Determine how many rows are returned based solely on similarity of the last name SELECT @HitCountLastNameOnly = COUNT(*) FROM PhysicianMstr p (NOLOCK) WHERE p.AN_CustID = @AN_CustID AND p.Active = 1 AND @SdxLastName = dbo.aif_Soundex(RTRIM(p.LastName))
-- Check if using just the last name soundex returns too few rows IF @HitCountLastNameOnly < @ThresholdLastName BEGIN -- Determine how many rows are returned based on last name soundex or first name soundex SELECT @HitCountFirstName = COUNT(*) FROM PhysicianMstr p (NOLOCK) WHERE p.AN_CustID = @AN_CustID AND p.Active = 1 AND ( @SdxLastName = dbo.aif_Soundex(RTRIM(p.LastName)) OR @TheFirstName = dbo.aif_Soundex(RTRIM(p.FirstName)) ) -- If too many rows, switch to exact match on first name instead of soundex of first name IF @HitCountFirstName > @ThresholdFirstName BEGIN SET @TheFirstName = @FirstName SET @HitCountLastNameOnly = -1 --negative value indicates first name exact match, not soundex END END
-- List physicians with similar sounding names SELECT p.AN_PhysicianID, pm.AN_CustID, RTRIM(p.LastName) AS LastName, RTRIM(p.FirstName) AS FirstName, RTRIM(p.StateLicNo) AS UPIN, RTRIM(mis.Specialty) AS Specialty1, pm.Specialty2, p.Active, RTRIM(pm.Degree) AS Degree, pm.Extra, p.CreatedBy, p.CreatedOn, p.ModifiedBy, p.ModifiedOn, p.PLEPhysicianMIID FROM PLEPhysicianMstrMI p (NOLOCK) INNER JOIN PhysicianMstr pm (NOLOCK) ON p.AN_PhysicianID = pm.AN_PhysicianID LEFT JOIN PLEPhysicianSpecialtyMstrMI mis (NOLOCK) ON p.PLEPhysicianSpecialtyMIID = mis.PLEPhysicianSpecialtyMIID WHERE pm.AN_CustID = @AN_CustID AND p.Active = 1 AND ( @SdxLastName = dbo.aif_Soundex(RTRIM(p.LastName)) OR @TheFirstName = CASE -- Include first name only when we don't have the minimum number of rows WHEN @HitCountLastNameOnly < 0 THEN p.FirstName --exact first name match WHEN @HitCountLastNameOnly < @ThresholdLastName THEN dbo.aif_Soundex(RTRIM(p.FirstName)) ELSE '****' --force false for first name test END ) ORDER BY p.LastName, p.FirstName
I have web forms with about 10-15 optional search parameters (fields) for a give table. Each item (textbox) in the form is treated as an AND condition.
Right now I build complex WHERE clauses based on wheather data is present in a textbox and AND each one in the clause. Also, if a particular field is "match any word", i get a ANDed set of OR's. As you can imagine, the WHERE clause gets quite large.
I build clauses like this (i.e., 4 fields shown):
SELECT * from tableName WHERE (aaa like '%data') AND (bbb = 'data') AND (ccc like 'data%') AND ( (xxx like '%data') OR (yyy like '%data%') )
My question is, are stored procedures better for building such dynamic SQL clauses? I may have one field or all fifteen. I've written generic code for building the clauses, but I don't know much about stored procedures and am wondering if I'm making this more difficult on myself.
Select hours from GetBaseStoredProc '10/01/2004' Where ReasonId = 1
where GetBaseStoredProc is a stored procedure that takes a date parameter and contains a select sql statement like: select reasonId, hours from my table where mydate = @myDate
(in reality, it's a much larger and more complicated statement, just using this as an example)
I guess I'm treating my storedProc like a view. If worse comes to worse, I suppose I could create a view (would rather not), but I'm wondering if what I want to do is possible, and I'm just not using the right syntax. Many thanks - (a former Oracle dev)
i have a vb app that is retriving data from an sql db using ado. i have a qry save in my db. i want to select from this qry where x in (1,2,3,4). i don't know how many values i will be putting into my where in statment. it could be one value or 100 values. below is my code. what i would like to do is pass one paramater to my stored proc and then break it up and use it in my select. my desired result is as follows
----------------------------------------------- declare cnt int declare TempFulLString nvarchar(5000) declare TempStyleFID int
TempFulLString = @str_TempString
select * from QryPicking_Slip_Fill_Listview1 where stylefid in (
I'm trying to call a stored proc with parameters without using EXEC statement and with only using a SELECT statement. I have the stored proc and need to call it from a 3rd party application which provides an interface to SQL server and does not support EXEC statements, but only SELECT statements. Is there a way this can be done?