WITH CTE AS (
SELECT
user_id,
ip_addr,
MIN(reg_date) AS min_reg_date
FROM
user_info
GROUP BY
ip_addr
HAVING
COUNT(*) > 1
)
SELECT
users.id
FROM
users
JOIN user_info
ON users.id = users_info.user_id
JOIN CTE
ON user_info.ip_addr = CTE.ip_addr
AND user_info.reg_date > CTE.min_reg_date;
user_info table:
CREATE TABLE `user_info` (
`user_id` int(11) UNSIGNED NOT NULL,
`ip_addr` varchar(255) DEFAULT NULL,
`user_agent` varchar(255) DEFAULT NULL,
`reg_date` timestamp NOT NULL DEFAULT current_timestamp()
)
users table:
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`nickname` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(255) NOT NULL,
`avatar` varchar(255) NOT NULL,
`group_id` int(11) UNSIGNED DEFAULT NULL
)
I tried using EXISTS and IN keywords but it shows that the error is near "DELETE FROM..."
Should i change something else ? Please help.
Tree Image
I want to create a multilevel tree structure for user Registration , a members can add up to 5 members if he adds another member which greater than the limit it should be added as the first child member. for example as in the figure tree Image, 1 can directly add members 2,3,4,5,6 if 1 add a member again it should be added as the first child of 2,and if 1 add again it should be added as the first child of 3 and so on up to first child of 6, then if one again adds it should be the second child of 2, again second child of 3 and so on how can i create a stored procedure to insert into my user table which automatically perform this here i am attaching my table structure also
Here I am attaching the table how can i achieve this
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Login_ID] [uniqueidentifier] NOT NULL,
[Login_Name] [nvarchar](100) NOT NULL,
[Login_Password] [nvarchar](100) NOT NULL,
[CreatedBy] [uniqueidentifier] NULL,
[ModifiedBy] [uniqueidentifier] NULL,
[Referal_ID] [uniqueidentifier] NULL,
[Name] [nvarchar](100) NULL,
[User_Address] [nvarchar](max) NULL,
[Phone] [nvarchar](14) NULL,
[Email] [nvarchar](100) NULL,
[BankName] [nvarchar](250) NULL,
[AccountName] [nvarchar](100) NULL,
[IFSC] [nvarchar](100) NULL,
[AccountNo] [nvarchar](150) NULL,
[DOB] [datetime] NULL,
[Created_Date] [datetime] NULL,
[Modified_Date] [datetime] NULL,
[Last_Login_Date_Time] [datetime] NULL,
[UnsuscribeEmail] [int] NULL,
[UnsuscribeSms] [int] NULL,
[IsBanned] [int] NULL,
[ISDeleted] [int] NULL,
PRIMARY KEY CLUSTERED ([Login_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
USE [Hyperbole]
GO
/****** Object: Table [dbo].[Users] Script Date: 9/26/2016 1:22:23 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Users](
[Login_ID] [bigint] IDENTITY(1,1) NOT NULL,
[Login_Name] [nvarchar](100) NOT NULL,
[Login_Password] [nvarchar](100) NULL,
[CreatedBy] [bigint] NULL,
[ModifiedBy] [bigint] NULL,
[Referal_ID] [bigint] NULL,
[Name] [nvarchar](250) NULL,
[User_Address] [nvarchar](max) NULL,
[Phone] [nvarchar](14) NULL,
[Email] [nvarchar](250) NULL,
[BankName] [nvarchar](250) NULL,
[AccountName] [nvarchar](250) NULL,
[IFSC] [nvarchar](250) NULL,
[AccountNo] [nvarchar](250) NULL,
[DOB] [datetime] NULL,
[Created_Date] [datetime] NULL,
[Modified_Date] [datetime] NULL,
[Last_Login_Date_Time] [datetime] NULL,
[UnsuscribeEmail] [int] NULL,
[UnsuscribeSms] [int] NULL,
[IsBanned] [int] NULL,
[ISDeleted] [int] NULL,
PRIMARY KEY CLUSTERED
(
[Login_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Stored Procedure
CREATE Proc [dbo].[User_SP]
(
#Login_Name nvarchar(100)=null
,#Login_Password nvarchar(100)=null
,#CreatedBy BIGINT=null
,#ModifiedBy BIGINT=null
,#Referal_ID BIGINT=null
,#Name nvarchar(250)=null
,#User_Address nvarchar(max)=null
,#Phone nvarchar(14)=null
,#Email nvarchar(250)=null
,#BankName nvarchar(250)=null
,#AccountName nvarchar(250)=null
,#IFSC nvarchar(250)=null
,#AccountNo nvarchar(150)=null
,#DOB datetime=null
,#Created_Date datetime=null
,#Modified_Date datetime=null
,#Last_Login_Date_Time datetime=null
,#UnsuscribeEmail int=0
,#UnsuscribeSms int=0
,#IsBanned int=0
,#ISDeleted int=0
)
--#UserName NVARCHAR (255),
--#parentID BIGINT
as
begin
IF OBJECT_ID('tempdb..#ChildHierarchy') IS NOT NULL
DROP TABLE #ChildHierarchy
IF OBJECT_ID('tempdb..#ChildCount') IS NOT NULL
DROP TABLE #ChildCount
CREATE TABLE #ChildHierarchy ( ChildId BIGINT, ChildName NVARCHAR(255), LevelNo BIGINT, ParentID BIGINT )
CREATE TABLE #ChildCount ( ChildParentID BIGINT, ChildCount BIGINT, ChildParentLevelID BIGINT, MaxChildCount BIGINT, AvailableChildCount BIGINT )
DECLARE #MaxChildCount TABLE ( LevelID INT, MaxChildCount BIGINT )
DECLARE #NextParentID BIGINT, #MaxLevelNo INT
INSERT INTO #MaxChildCount (LevelID, MaxChildCount)
VALUES ( 1, 5 ), ( 2, 25 ), ( 3, 125 ), ( 4, 625 ), ( 5, 3125 ), ( 6, 15625 ), ( 7, 78125 ), ( 8, 390625 ), ( 9, 1953125 ), ( 10, 9765625 )
/*row generations*/
;WITH Hierarchy(ChildId, ChildName, LevelNo, ParentId)
AS
(
SELECT Login_ID, Name, 0, Referal_ID
FROM Users AS FirtGeneration
WHERE Login_ID = #Referal_ID
UNION ALL
SELECT NextGeneration.Login_ID, NextGeneration.Name, Parent.LevelNo + 1, Parent.ChildId
FROM Users AS NextGeneration
INNER JOIN Hierarchy AS Parent ON NextGeneration.Referal_ID = Parent.ChildId
)
INSERT INTO #ChildHierarchy (ChildId,ChildName,LevelNo,ParentID)
SELECT ChildId, ChildName, LevelNo, ParentId
FROM Hierarchy
OPTION(MAXRECURSION 32767)
INSERT INTO #ChildCount (ChildParentID, ChildCount,ChildParentLevelID)
SELECT ParentID, COUNT(ChildId), LevelNo
FROM #ChildHierarchy
WHERE LevelNo > 0
GROUP BY ParentID, LevelNo
UPDATE CC SET MaxChildCount = MCC.MaxChildCount
FROM #ChildCount CC
INNER JOIN #MaxChildCount MCC ON CC.ChildParentLevelID = MCC.LevelID
SET #MaxLevelNo = ( SELECT MAX(LevelNo) FROM #ChildHierarchy )
UPDATE CC SET AvailableChildCount = CC1.AvailableChildCount
FROM #ChildCount CC
INNER JOIN ( SELECT ChildParentLevelID,SUM(ChildCount) AS AvailableChildCount FROM #ChildCount GROUP BY ChildParentLevelID ) CC1 ON CC.ChildParentLevelID = CC1.ChildParentLevelID
IF #MaxLevelNo = 0 OR NOT EXISTS ( SELECT TOP 1 1 FROM #ChildCount )
SET #NextParentID = #Referal_ID
IF #NextParentID IS NULL
SET #NextParentID = ( SELECT MIN(ChildParentID) FROM #ChildCount WHERE MaxChildCount <> AvailableChildCount AND ChildCount <> 5 )
IF #NextParentID IS NULL
SET #NextParentID = ( SELECT MIN(CH.ChildId) FROM #ChildHierarchy CH WHERE NOT EXISTS ( SELECT TOP 1 1 FROM #ChildCount CC WHERE CH.ChildId = CC.ChildParentID ) )
INSERT INTO Users (Login_Name,Login_Password,CreatedBy,ModifiedBy,Referal_ID,Name,User_Address,Phone,Email,BankName,AccountName,IFSC,AccountNo,DOB,Created_Date,Modified_Date,UnsuscribeEmail,UnsuscribeSms,IsBanned,ISDeleted)
SELECT #Login_Name,#Login_Password,#NextParentID,#NextParentID,#NextParentID,#Name,#User_Address,#Phone,#Email,#BankName,#AccountName,#IFSC,#AccountNo,#DOB,#Created_Date,#Modified_Date,0,0,0,0
end
GO
I have a table that contains a few columns bound to a gridview.
In that gridview, I have an edit option to update the columns. In that situation I need to write a two update stored procedures that means I select all columns expect AudiotoName, select another columns all columns are update to raise one update query but when I select table in that have AudiotoName column that only edit to select that column it will raise second update stored procedure. I tried but it not properly working can anyone help me out.
My code:
ALTER PROCEDURE up_file
(#ModuleID int,
#SubjectID int,
#Physician varchar(500) = '',
#AuditoName varchar(300) = '',
#AuditoType varchar(50) = '',
#AudioPath varchar(2000) = '',
#BaseDocumentName varchar(500) = '',
#BaseDocumentPath varchar(2000) = '',
#Createddate datetime,
#CreatedBy varchar(200) = '')
AS
BEGIN
IF #AuditoName = 'true' //select AuditoName column only raise this update query
BEGIN
UPDATE SubjectItems
SET ModuleID = #ModuleID,
SubjectID = #SubjectID,
Physician = '#Physician',
AuditoName = '#AuditoName',
AuditoType = '#AuditoType',
AudioPath ='#AudioPath',
BaseDocumentName = '#BaseDocumentName',
BaseDocumentPath = '#BaseDocumentPath'
WHERE AuditoName = #AuditoName
END
BEGIN //normal fields select raise this update query
UPDATE SubjectItems
SET ModuleID = #ModuleID,
SubjectID = #SubjectID,
Physician = '#Physician',
AuditoName = '#AuditoName',
AuditoType = '#AuditoType',
AudioPath ='#AudioPath',
BaseDocumentName = '#BaseDocumentName',
BaseDocumentPath = '#BaseDocumentPath'
WHERE ModuleID = #ModuleID
END
END
Can anyone help me out?
The problem in your query is that, even if #AuditoName is true, the lower update query is running. This will re-update the table SubjectItems. You can use if...else block instead, like below:
ALTER PROCEDURE up_file
(#ModuleID int,
#SubjectID int,
#Physician varchar(500) = '',
#AuditoName varchar(300) = '',
#AuditoType varchar(50) = '',
#AudioPath varchar(2000) = '',
#BaseDocumentName varchar(500) = '',
#BaseDocumentPath varchar(2000) = '',
#Createddate datetime,
#CreatedBy varchar(200) = '')
AS
BEGIN
IF #AuditoName = 'true' //select AuditoName column only raise this update query
BEGIN
UPDATE SubjectItems
SET ModuleID = #ModuleID,
SubjectID = #SubjectID,
Physician = '#Physician',
AuditoName = '#AuditoName',
AuditoType = '#AuditoType',
AudioPath ='#AudioPath',
BaseDocumentName = '#BaseDocumentName',
BaseDocumentPath = '#BaseDocumentPath'
WHERE AuditoName = #AuditoName
END
ELSE
BEGIN //normal fields select raise this update query
UPDATE SubjectItems
SET ModuleID = #ModuleID,
SubjectID = #SubjectID,
Physician = '#Physician',
AuditoName = '#AuditoName',
AuditoType = '#AuditoType',
AudioPath ='#AudioPath',
BaseDocumentName = '#BaseDocumentName',
BaseDocumentPath = '#BaseDocumentPath'
WHERE ModuleID = #ModuleID
END
END
I am trying to Insert, Update, Delete using single stored procedure. Insertion is working correctly but for Deletion it raise error that-
#ID is not a parameter for procedure hrm_Langauges.
I am trying to delete using the id column.
Here is my stored procedure.
ALTER PROCEDURE [dbo].[hrm_Langauges]
(
#Name varchar(120) = 0,
#CreatedOn datetime = 0,
#UpdatedOn datetime = 0,
#CreatedBy bigint = 0,
#UpdatedBy bigint = 0,
#IsDeleted bit = 0,
#status as varchar(50)
)
AS
Declare #ID int;
Select #ID = count(ID) + 1 from [dbo].[Languages]
if(#status = 'Display')
BEGIN
SELECT ID FROM [dbo].[Languages] WHERE Name=#Name
END
else if(#status = 'Add')
BEGIN
IF EXISTS(SELECT Name FROM [dbo].[Languages] WHERE Name = #Name and IsDeleted=0)
Begin
Return 0
End
Else
INSERT INTO [dbo].[Languages](Name,CreatedOn,CreatedBy) VALUES(#Name,#CreatedOn,#CreatedBy)
END
else if(#status = 'Update')
BEGIN
UPDATE [dbo].[Languages] Set Name=#Name,UpdatedOn=#UpdatedOn, UpdatedBy=#UpdatedBy WHERE ID=#ID
END
else if(#status = 'Delete')
BEGIN
UPDATE [dbo].[Languages] Set IsDeleted=#IsDeleted WHERE ID=#ID
END
Where I have to change my sp.
Please help me.
As per your comment,
I am passing id parameter from asp code. Delete record for that Id.
and
Yes I am passing Id from code. Where to change in sp so that it work for that parameter
Your SP parameters don't have #ID, you have declared it locally.
I want you to check if you are trying to passing #Idas parameter to SP. If so, it is cause of error, as SP parameters don't have any parameter named #Id in parameters list.
Solution is to add parameter like #Id INT =0 in parameter.
Also you you have to rename local parameter #Id & all of its usage as this can conflict.
ALTER PROCEDURE [dbo].[hrm_Langauges]
(
#Name varchar(120) = 0,
#CreatedOn datetime = 0,
#UpdatedOn datetime = 0,
#CreatedBy bigint = 0,
#UpdatedBy bigint = 0,
#IsDeleted bit = 0,
#status as varchar(50)
,#Id INT =0 //Add this line
)
AS
Declare #ID_Local int;//Change
Select #ID_Local = count(ID) + 1 from [dbo].[Languages]//change
if(#status = 'Display')
BEGIN
SELECT ID FROM [dbo].[Languages] WHERE Name=#Name
END
else if(#status = 'Add')
BEGIN
IF EXISTS(SELECT Name FROM [dbo].[Languages] WHERE Name = #Name and IsDeleted=0)
Begin
Return 0
End
Else
INSERT INTO [dbo].[Languages](Name,CreatedOn,CreatedBy) VALUES(#Name,#CreatedOn,#CreatedBy)
END
else if(#status = 'Update')
BEGIN
UPDATE [dbo].[Languages] Set Name=#Name,UpdatedOn=#UpdatedOn, UpdatedBy=#UpdatedBy WHERE ID=#ID_Local//change
END
else if(#status = 'Delete')
BEGIN
UPDATE [dbo].[Languages] Set IsDeleted=#IsDeleted WHERE ID=#ID
END
I have these tables:
doodhiya
dhid INTEGER PRIMARY KEY NOT NULL,
dname TEXT NOT NULL,
dfname TEXT NOT NULL,
dage INTEGER NOT NULL,
dadd TEXT,
dphone INTEGER NOT NULL,
demail TEXT NOT NULL
doodhdata
dtid INTEGER PRIMARY KEY NOT NULL,
ddate INTEGER NOT NULL,
dmonth INTEGER NOT NULL,
dyear INTEGER NOT NULL,
dmilk INTEGER NOT NULL,
dprice INTEGER NOT NULL default 35 ,
dmore TEXT,
ddhid INTEGER NOT NULL
pricemilk
pid INTEGER PRIMARY KEY NOT NULL,
pmonth INTEGER NOT NULL,
pyear INTEGER NOT NULL,
milkprice INTEGER NOT NULL,
typeperson TEXT,
userid INTEGER,
gheeprice INTEGER,
defaultprice TEXT
cashdata
cashid INTEGER PRIMARY KEY NOT NULL,
cashdate INTEGER NOT NULL,
cashmonth INTEGER NOT NULL,
cashyear INTEGER NOT NULL,
cashamount INTEGER NOT NULL,
uid INTEGER NOT NULL,
utype TEXT NOT NULL,
cashtype TEXT NOT NULL,
cashmore TEXT
I want to make a monthly bill and I am succeed buy using it... but in a bill how can i show last month balance....I am trying to use it
SELECT
ddhid, dmonth, dyear, dmilk,
userid, pmonth, pyear, milkprice,
uid, cashmonth, cashyear, cashamount, utype,
SUM(dmilk) AS totalmilk,
SUM(dmilk*milkprice) AS totalamount,
SUM(cashamount) AS totalcash
FROM
doodhdata
LEFT JOIN pricemilk ON (
doodhdata.ddhid = pricemilk.userid
AND doodhdata.dmonth = pricemilk.pmonth
AND doodhdata.dyear = pricemilk.pyear
)
LEFT JOIN cashdata ON (
doodhdata.ddhid = cashdata.uid
AND doodhdata.dmonth = cashdata.cashmonth
AND doodhdata.dyear = cashdata.cashyear
)
WHERE
dmonth > '$mikdatem'
AND dyear='$mikdatey'
AND ddhid='$dhid'
But I want to use defaultprice when milkprice is NULL....how it is possible...?
Use
COALESCE(milkprice, defaultprice)
in place of milkprice in your query.
See SQLite core functions documentation.