我有一个测试表
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
struct Node* modifyTheList(struct Node *head);
void push(struct Node **head_ref, int new_data)
{
struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
void printList(struct Node *head)
{
if (!head)
return;
while (head->next != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout << head->data << endl;
}
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
struct Node *head = NULL;
while(n--){
int a;
cin>>a;
push(&head, a);
}
head = modifyTheList(head);
printList(head);
}
return 0;
}
void modify(Node *head ,Node** left)
{
if(head!=NULL)
{
modify(head->next,left);
if(head >= *left ) //line of concern, comparing pointers
return;
else
{
(*left)->data=(*left)->data-head->data;
(*left)=(*left)->next;
}
}
}
struct Node* modifyTheList(struct Node *head)
{
if(head==NULL || head->next==NULL )
return head;
struct Node *temp = head;
modify(head,&temp);
return head;
}
和问题表格为
CREATE TABLE [dbo].[Test](
[TestId] [int] IDENTITY(1,1) NOT NULL,
[TestName] [nvarchar](50) NOT NULL,
[UserId] [int] NOT NULL,
[isDelete] [bit] NOT NULL,
问题表以Tid作为外键存储每个测试的所有问题。
我想编写一个存储过程来在单个存储过程中获取TestName,TestId和每个测试中的问题数。但我无法得到这个。
答案 0 :(得分:0)
您可以将存储过程编写为:
CREATE PROCEDURE [dbo].[procGetNumberofQuestionsForTest]
AS
BEGIN
SELECT T.[TestId], T.[TestName], COUNT(Q.[Qid]) AS NumberOfQuestions
FROM [dbo].[Test] T
JOIN [dbo].[Questions] Q ON Q.Tid = T.TestId
GROUP BY T.[TestId], T.[TestName]
END
如果您想获得特定测试的结果,请将参数作为@TestId INT
传递,并在WHERE
之前将WHERE T.[TestId] = @TestId
子句添加为GROUP BY
。
答案 1 :(得分:0)
试试这个(包括表创建,插入,proc创建和执行);
CREATE TABLE [dbo].[Test](
[TestId] [int] IDENTITY(1,1) NOT NULL,
[TestName] [nvarchar](50) NOT NULL,
[UserId] [int] NOT NULL,
[isDelete] [bit] NOT NULL)
go
CREATE TABLE [dbo].[Questions](
[Qid] [int] IDENTITY(1,1) NOT NULL,
[Tid] [int] NOT NULL,
[Qtype] [int] NOT NULL,
[Question] [nvarchar](max) NOT NULL,
[isDelete] [bit] NULL
)
go
insert into [dbo].[Test]
values('test #1',1,0)
go
insert into [dbo].[Questions]
values(1,1,'what is life',0)
go
create proc dbo.MyInfo
as
select
t.TestName,
t.TestId,
[No Questions]=COUNT(q.Qid)
from
[dbo].[Test] t
inner join
[dbo].[Questions] q on t.TestId=q.Qid
group by
t.TestName,
t.TestId
go
exec dbo.MyInfo
go