i have 3 tables:
payorderType :
---------
typeID | TypeName |
1 | accounting |
2 | budget |
----------
step:
----------
StepID | StepName | typeID
1 | payorder | 1
2 | cheque | 1
3 | cheque | 2
----------
user:
----------
userID | StepName | StepID
7878 | payorder | 1
4547 | cheque | 2
6538 | cheque | 1
----------
I want to make a table which users exists in row and columns includes with concat of step and payorderType. same as below:
users | accounting_payorder | accounting_cheque | budget_cheque |
7878 | 1 | 0 | 0 |
4547 | 0 | 1 | 0 |
6538 | 0 | 1 | 0 |
My quesdtion is : if i don't know number of payorderType rows and number of step rows, how should i write it? My Script is here : First I create a table in cursor for concat payorderType and step:
CREATE PROC sp_payOrderType
AS
BEGIN
DECLARE a CURSOR
FOR SELECT DISTINCT p.TypeName,s.StepName
FROM
dbo.PayOrderType p LEFT JOIN
dbo.vStep s ON s.TypeID = p.TypeID
FOR READ ONLY
DECLARE @payOrderType NVARCHAR(50),@stepName NVARCHAR(50)
DECLARE @SQL NVARCHAR(MAX)=''
OPEN a
FETCH NEXT FROM a INTO @payOrderType, @stepName
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE @b VARCHAR(max), @b2 VARCHAR(max)
SELECT @b = ISNULL(@b ,'') +'['+ ISNULL(@payOrderType ,'')+ '____'+ISNULL(@stepName ,'')+ ']'+ ' NVARCHAR(1000) ,'
FETCH NEXT FROM a INTO @payOrderType,@stepName
END
CLOSE a
DEALLOCATE a
SELECT @SQL = 'ALTER table AA(' + SUBSTRING(@b,1, LEN(@b)-1) + ')'
SELECT @SQL
END
but i don't know how i should relate rows(userID) with columns ?