Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对Insert语句值使用Select语句_C#_Asp.net_Visual Studio 2010 - Fatal编程技术网

C# 对Insert语句值使用Select语句

C# 对Insert语句值使用Select语句,c#,asp.net,visual-studio-2010,C#,Asp.net,Visual Studio 2010,您能帮我处理一下这个语句吗?我正在尝试使用SELECT语句检索数据,并在INSERT语句中使用日期 我想使用为ProfileId值检索的数据 如何使用SELECT结果作为ProfileId值。insert语句应该是 插入用户\u FriendProfileId1,ProfileId 值@FriendProfileId, 从User_Profile中选择ProfileId,其中UserId=@UserId 或者选择TOP1 ProfileId以确保您永远不会获得超过1个值。插入SQL应该是: st

您能帮我处理一下这个语句吗?我正在尝试使用SELECT语句检索数据,并在INSERT语句中使用日期

我想使用为ProfileId值检索的数据


如何使用SELECT结果作为ProfileId值。

insert语句应该是

插入用户\u FriendProfileId1,ProfileId 值@FriendProfileId, 从User_Profile中选择ProfileId,其中UserId=@UserId
或者选择TOP1 ProfileId以确保您永远不会获得超过1个值。

插入SQL应该是:

string insertSql = "INSERT INTO User_Friend(ProfileId1, ProfileId) SELECT @FriendProfileId, ProfileId FROM User_Profile WHERE UserId = (@UserId)";

您只需将变量直接包含在SELECT语句中与列名对应的位置。

可能的重复感谢您的重播,我只是从userId中选择ProfileId,而不是两者。当您包含这样的参数时,它不会从用户表中选择,而是直接在SQL中替换值。假设@FriendProfileId设置为23,SELECT语句实际上是SELECT 23,ProfileID来自用户_profile。。。
string insertSql = "INSERT INTO User_Friend(ProfileId1, ProfileId) SELECT @FriendProfileId, ProfileId FROM User_Profile WHERE UserId = (@UserId)";