I want to generate a sequence number based on the column value. I want to have this kind of output. I gonna use this in c# .net winform as GridView output
TABLE1
ID Name NoStub
1 arte 3
2 gonzake 2
TABLE2
ID Name StubNumberStart StubNumberEnd
1 arte 0001 0003
2 gonzake 0004 0005
Try this query.. it will give result from Table 2 to Table 1
DECLARE #T1 AS TABLE (ID INT, NAME VARCHAR(50), STUBNUMBER VARCHAR(10))
INSERT INTO #T1 VALUES ( 1, 'ARTE', '001')
INSERT INTO #T1 VALUES ( 1, 'ARTE', '002')
INSERT INTO #T1 VALUES ( 1, 'ARTE', '003')
INSERT INTO #T1 VALUES ( 1, 'GONZAKE', '004')
INSERT INTO #T1 VALUES ( 1, 'GONZAKE', '005')
SELECT * FROM #T1
SELECT DISTINCT ID ,NAME, COUNT(*) AS NOSTUB FROM #T1
GROUP BY ID, NAME
If your request is different from Table 1 to Table 2 then please let me know .. you will get new query...
ALTER PROCEDURE ExpandIt
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Id int;
DECLARE #name varchar(50);
DECLARE #noStub int;
DECLARE #stubNumber char(8);
DECLARE #count as int = 0;
DECLARE #continuedID as int = 0;
DECLARE t1 CURSOR FAST_FORWARD FOR
SELECT ID, Name,NoStub from Table1
OPEN t1
FETCH NEXT FROM t1 INTO #Id, #name, #noStub
WHILE ##FETCH_STATUS = 0
BEGIN
WHILE (#count < #noStub)
BEGIN
SET #count = #count + 1;
SET #stubNumber = ('0000' + CONVERT (CHAR, #continuedID + #count));
SET #stubNumber = SUBSTRING (#stubNumber,LEN(#stubNumber)-4+1, 4);
INSERT INTO Table2 (ID, Name, StubNumber)
VALUES (#Id, #name,#stubNumber);
END
SET #continuedID = #count;
SET #count = 0;
FETCH NEXT FROM t1 INTO #Id, #name, #noStub
END
CLOSE t1 ;
DEALLOCATE t1
END
Related
I am trying to update a table using table type parameter. Currently I am using the below query. But I would like to perform update in batches having batch size as one of the parameters. Kindly help.
ALTER PROCEDURE UPDATEStatus
#Ids int ,
#numbers TypeofNumbers readonly,
#Status char(2),
#nname varchar(50),
AS
BEGIN
BEGIN TRY
update e
set
e.status = #Status,
e.user =#nname,
e.time = GETDATE()
from detailtable e
join #numbers en on en.ID =e.ID
where e.oddIDs = #Ids
I tried to do in a single update but I wanted to do in sets or batches one by one. say 100 records first and then next 100 records until all are done
You can use something like this to do your update in batches:
CREATE OR ALTER PROCEDURE UPDATEStatus
#Ids INT,
#numbers TypeOfNumbers READONLY,
#Status CHAR(2),
#nname VARCHAR(50)
AS
BEGIN
DECLARE #UpdatedRows INT;
DECLARE #Skip INT = 0;
DECLARE #BatchSize INT = 100;
WHILE ISNULL(#UpdatedRows, 1) > 0
BEGIN
WITH CTE
AS (SELECT *
FROM #numbers AS n
ORDER BY n.ID OFFSET #Skip * #BatchSize ROWS FETCH NEXT #BatchSize ROWS ONLY)
UPDATE e
SET
e.[Status] = #Status,
e.[User] = #nname,
e.[time] = GETDATE()
FROM CTE AS en
JOIN detailtable e ON en.ID = e.ID;
SET #UpdatedRows = ##ROWCOUNT;
SET #Skip = #Skip + 1;
END;
END;
GO
Next time please also provide scripts for the DDL and some testdata.
I have 3 SQL Objects:
Table Employee with three fields (id, name, salary)
Procedure Insert_employee [to insert in employee table]
Trigger [to make validation for table]
My problem: if call Insert_employee by Entity Framework,
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.
ALTER PROC [dbo].[Insert_Employees]
(
#id AS INT,
#name AS NVARCHAR(max),
#salary AS NUMERIC(18,3)
)
AS
BEGIN
DECLARE #error AS NVARCHAR(max)
BEGIN TRANSACTION
INSERT INTO employee(Id, Name,Salary)
SELECT #id, #name , #salary
SELECT #error = ##ERROR
IF #error = 0 COMMIT ELSE ROLLBACK
END
ALTER TRIGGER [dbo].[INS_employee] ON [dbo].[Employee]
INSTEAD OF INSERT
AS
BEGIN
IF EXISTS (SELECT * FROM inserted WHERE inserted.name = '')
BEGIN
RAISERROR('name is empty',16,1);
END
ELSE
INSERT INTO employee(id, Name, Salary)
SELECT id, Name, Salary FROM inserted
END
Assume i got the below row of number and max quantity value is 10.
Quantity BatchValue
2 0
4 0
4 0
6 1
8 2
Summation of 2+4+4 gives me a value less than or equal to max quatity 10 and so the batch value for those rows become 0. The pending rows are 6 and 8. They cannot be summed up to be < max quantity. So they will be seperate. Can we get an sql query or an algorith that can do this?
Here's a nice running sum routine you can use
create table #temp (rowid int identity, quantity int)
insert #temp
select quantity from yourtable order by your order
declare #holding table (quantity int, runningsum int)
declare #quantity int
declare #running int=0
declare #iterator int = 1
while #iterator<=(select max(rowid) from #temp)
begin
select #quantity=quantity from #temp where rowid=#iterator
set #running=#quantity+#running
insert #holding
select #quantity, #running
set #iterator=#iterator+1
end
Edited code from Daniel Marcus above to give the actual response requested in query.
CREATE TABLE #temp(rowid int identity(1,1), quantity int)
INSERT INTO #temp
SELECT 2
UNION ALL SELECT 4
UNION ALL SELECT 4
UNION ALL SELECT 6
UNION ALL SELECT 8
declare #batchValue int = 0 ,#maxquantity int = 10
declare #holding table (quantity int, batchvalue int)
declare #quantity int
declare #running int=0
declare #iterator int = 1
while #iterator<=(select max(rowid) from #temp)
begin
select #quantity=quantity from #temp where rowid=#iterator
set #running=#quantity+#running
-- Newly added condition
if (#running > #maxquantity) BEGIN
SET #batchValue = #batchValue + 1 -- increment the batch value
insert #holding select #quantity, #batchValue
SET #running = #quantity -- reset the running value
END
ELSE
insert #holding select #quantity, #batchValue
set #iterator=#iterator+1
end
SELECT * FROM #holding
DROP TABLE #temp
Hope the snippet works for your purpose. I tested this in SQL azure and provides the result you mentioned.
--The below query will help you if you working on sql server 2012 or higher
CREATE TABLE #RUN_TOT(ID INT)
INSERT INTO #RUN_TOT VALUES(2),(4),(4),(6),(8)
;WITH CTE AS
(
SELECT ID,
ROW_NUMBER() OVER(ORDER BY ID) RNUM,
CASE
WHEN SUM(ID) OVER(ORDER BY ID ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) <=
(SELECT MAX(ID) FROM #RUN_TOT) THEN 0
ELSE
ID
END VAL
FROM #RUN_TOT
)
SELECT ID,VAL FROM CTE WHERE VAL=0
UNION ALL
SELECT ID,ROW_NUMBER() OVER(ORDER BY VAL) VAL FROM CTE WHERE VAL<>0
I have a DB with 60 tables. In some of those tables I have a bit column named "Open" where I store a 0 if the record is not in use by an user (user access DB from a C# application) and an 1 if the record is in use.
Well, I need to get all the records from all the tables in my database where the "open" column value is true, or 1.
Is this even possible to do?
Here's a simple use of the undocumented sp_MSforeachtable:
EXEC sp_MSforeachtable
#command1='SELECT * FROM ? WHERE Open=1',
#whereand='AND o.id in (select object_id from sys.columns c where c.name=''Open'')'
Quick piece of code that gets list of tables within your database. Using a cursor loop through the answers checking it they have the fld named [open] and if it does the build a SQL statement and the execute this SQL string.
CREATE PROCEDURE usp_BulkTableOpenReport
AS
BEGIN
DECLARE #TBLS AS TABLE (REF INT IDENTITY (0,1), TABLENAME NVARCHAR(100), TABLEID BIGINT);
DECLARE #TBL AS NVARCHAR(100);
DECLARE #TBLID AS BIGINT;
DECLARE #SQL AS NVARCHAR(MAX);
DECLARE #I INT = 0;
DECLARE #M INT = 0;
DECLARE #V INT = 0
INSERT INTO #TBLS(TABLENAME,TABLEID)
SELECT NAME,OBJECT_ID FROM sys.tables
SELECT #M = MAX(REF) FROM #TBLS
WHILE #I <= #M
BEGIN
SELECT #TBL = TABLENAME, #TBLID= TABLEID FROM #TBLS WHERE REF = #I
/* CHECK TO MAKE INSURE THAT A FLD CALLED [OPEN] EXIST. */
SELECT #V = COUNT(*) FROM SYS.columns WHERE name = 'OPEN' AND OBJECT_ID = #TBLID
IF #V != 0
BEGIN
SET #SQL = 'SELECT * FROM [' + #TBL + '] WHERE [OPEN] = 1'
EXEC SP_EXECUTESQL #SQL
END;
SET #I = #I + 1
END;
END
GO
From your c# application exec the query "EXEC usp_BulkTableOpenReport" then loop through the table outputs.
I want help from you to create a query. I am new in software career and i am trying to developing a KBC Type Quiz Game for Windows Mobile Application. For That i want try to fetch random records from database and if one record is appear at one time after that record will not appear again. I use This query "SELECT TOP 1 * FROM Quiz ORDER BY NEWID()" but records are repeat.
My Table structure is given below.
ColumnName DataType
Id int
Que varchar(150)
Ans1 varchar(100)
Ans2 varchar(100)
Ans3 varchar(100)
TrueAns varchar(100)
I try to create a Store Procedure also which is given below
DECLARE #counter int, #randno int, #uBound int, #lBound int
SELECT #uBound = Max(Id) FROM Quiz
SELECT #lBound = Min(Id) FROM Quiz
SELECT #randno = Round(((#uBound - #lBound) * Rand() + #lBound), 0)
SET #Counter = 0
WHILE #counter = 0
BEGIN
IF EXISTS(SELECT Id FROM Quiz WHERE Id = #randno)
BEGIN
SET NOCOUNT OFF
SELECT * FROM Quiz WHERE Id = #randno
SET #counter = 1
END
ELSE
BEGIN
SELECT #randno = Round(((#uBound - #lBound -1 ) * Rand() + #lBound), 0)
END
END
but i can't get success. My Table contain this fields Que, Ans1, Ans2, Ans3, TrueAns. Please Help me for this problem. And i want to also create a web-service which return all the records
just add a column in Quiz isread as bool and set it as a default false
then make your stored procedure like it will help you to go with webservice approach
DECLARE #counter int, #randno int, #uBound int, #lBound int
SELECT #uBound = Max(Id) FROM Quiz
SELECT #lBound = Min(Id) FROM Quiz
SELECT #randno = Round(((#uBound - #lBound) * Rand() + #lBound), 0)
SET #Counter = 0
WHILE #counter = 0
BEGIN
If(( select count(*) from Quiz where isread= true)=( select count(*) from Quiz ))
BEGIN
update Quiz SET isread=false
End
IF EXISTS(SELECT Id FROM Quiz WHERE Id = #randno and isread=false )
BEGIN
update Quiz SET isread=true WHERE Id = #randno
SET NOCOUNT OFF
SELECT * FROM Quiz WHERE Id = #randno
SET #counter = 1
END
ELSE
BEGIN
SELECT #randno = Round(((#uBound - #lBound -1 ) * Rand() + #lBound), 0)
END
END