I am doing a matrix multiplication project which involves with matrix
So here is my table format:
create table A ( row integer, col integer, val integer);
What i need to do is to fill in each table with data,
Task 1: create a 200*200 matrix A and all of its element initialized to 1.
That means I would do manually
insert<0,0,1> <0,1,1> <0,2,1> ….<0,199,1>
insert<1,0,1> <1,1,1> <0,2,1> …..<1,199,1> and so
Task 2: create 200*200 matrix A and its diagonal elements are 1’s.
So I am wondering instead of doing this manually, if there anyway to automate initialize all the elements?
The following is an attempt using while loop:
create table A ( row integer, col integer, val integer);
DECLARE @count INT
SET @count = 0
DECLARE @count2 INT
SET @count2 = 0
WHILE (@count < 200)
BEGIN
WHILE (@count2 <200)
BEGIN
INSERT INTO A([row], [col]) VALUES (@count, @counts)
SET @count2 = (@count2 + 1)
END
SET @count = (@count + 1)
END
This the above correct? I am fairly new to sql:(
Avoid loops and iterative stuff. You’re in SQL Server – think set-based
Try this
For your second requirement, you can be lazy and add
If it’s a one-off thing then this ought to be ok.
The
master..spt_valuesbit is all about getting a range of numbers between 1 and 200. You can also have this variation: