I have a Message table and a User table. Both are in separate databases. There is a userID in the Message table that is used to join to the User table to find things like userName.
How can I create this in LINQ to SQL? I can't seem to do a cross database join.
Should I create a View in the database and use that instead? Will that work? What will happen to CRUD against it? E.g. if I delete a message - surely it won't delete the user? I'd imagine it would throw an error.
What to do? I can't move the tables into the same database!
A view will work, if you have granted access to both database to the configured user. You'll need to use the 2-dot notation. This will only work BTW if both databases are on the same server.
create view vwUserMessages as
select * from db1.dbo.Users as users
inner join db2.dbo.Messages as msg on msg.UserID = users.id
For CRUD: a view is (usualy) only for reading: do updates etc directly to the related tables, or use a stored procedure:
create proc pdeleteUserMessages (#UserID int) as
begin trans
delete db2.dbo.Messages where userid = #UserID
delete db1.dbo.Users where id = #UserID
commit trans
go
Related
I am using the query band so the user can connect through the proxy user to the data pool in Teradata. Proxy users have full access to the table. How can I force the external users to see the selective columns? This is my query:
SET QUERY_BAND='PROXYUSER=TheAdmin; PROXYROLE=RoleOne;' FOR TRANSACTION;
SELECT Employee_Id, First_name, Last_Name FROM Project.myTest;
This query works, but the problem is if the external user use SELECT * FROM Project.myTest;
It returns the whole table. I want that user should not be able to see all the columns.
I don't want to use the view as I have a big DB and there I have to create so many views in each table.
Is there any way to do that? Will be great help and thanks in advance.
I have a database normalized like the one on the picture bellow. I store the clients, the accounts and the postal address for each account. As you can notice, the Client-Account relationship type is many-to-many, there for the ClientAccount table.
Since I have a database with 100k Account records I'm considering the use of SQL Bulk Copy. I think that I could use a stage table with all the fields of the tables above, and then normalize the data.
My problem is that I don't know how to move the data to the production tables. How can I create a stored procedure to perform this job, after the bulk insert import?
PS: The database is for as ASP .Net Web Site with the EF enabled.
You must insert one table at a time in the right order
Address -> Account -> Client -> AccountClient
create the addresses:
Insert into Address(data)
Select distinct address_data From temp
Account
Inser Into Account(account_number, address_id)
Select t.account_number, a.id From temp t
Inner Join Address a on a.data = t.address_data
Client
Insert into Client(name, client_number)
Select distinctname, client_number From temp
Client Account
Inser Into ClientAccount(accountId, clientId)
Select a.accountId, c.clientId From temp t
Inner Join Client c on c.name = t.name and c.client_number = t.client_number
Inner Join Account a on a.account_number= t.account_number
Make sure:
you don't create duplicate if some of the data are already there
you match on rows between real and temp table on the right columns
An Oracle DB user has SELECT permission on all the tables of a DB schema. Can i restrict the user to view the table data.The user should be able to select the table but should not be able to see the data.
This specific requirement is required for user who reviews DB design and generates ALTER script for DB using Oracle Data Modeler 3.3 where he can just see the table design and can compare it with ERD
Can i achieve it using FGAC or RLS?
You can achieve this by granting only references
GRANT references ON schema_a.table TO erd_user;
the erd_user can then use
DESC schema_a.table
to get a definition but not select any data.
This might be preferred to giving the SELECT CATALOGUE where they can see a lot more information that you might like.
I need to create a view in SQL Server and to use it in a GridView on my .aspx page.
The view is mapped in my application through Entity Framework.
Here's my example:
SELECT User.UserName, User.UserId
FROM User
WHERE NOT EXISTS (SELECT UsersInRoles.RoleId FROM UsersInRoles
WHERE UsersInRoles.UserId = User.UserId AND UsersInRoles.RoleId = 'AdminRole')
The relation between User and UsersInRoles table is one-to-many.
I need to be able to pass the AdminRole value in my ASP.NET application.
I cannot use parameters in a view (i.e. using #Role is not allowed).
Only views and tables can bind to a GridView so a stored procedure (where a parameter
could be an option) is not good here.
Is there a workaround to use a LEFT OUTER JOIN maybe instead of the NOT EXISTS?
This way, having a JOIN on the UsersInRoles table, I could map the fields from UsersInRoles through Entity Framework in my application and I could add conditions there (with EntityDataSource.Where clause).
For Exists, use the Any extension method in LINQ. For Not Exists, use !Any.
var query = from user in User
where !user.UsersInRoles.Any(role => role.RoleId == 'AdminRole')
select new {user.UserName, user.UserId};
I'm quite new in EF, but I know that we can use stored procedures (or functions) in it.
Try read this
Had a recent issue with my CMS and discovered today that all of my users created in the last week were deleted. The good news is that I have a backup of the DB, and that it uses standard ASP.NET Membership tables.
Is there a way to do a restore of this data without restoring the whole DB? The membership tables are something of a maze... is there an existing stored proc or utility out there? I'm not sure which tables would be required and which ones would not.
I'm not aware of any single sproc or utility to do this.
If you're using just the Membership, you'll need to copy from:
aspnet_Membership
aspnet_Users
If you're also using Roles you'll need to copy from:
aspnet_UsersInRoles
If you're also using Profile you'll need to copy from:
aspnet_Profile
So basically it's not that bad. You just need to roll up your sleeves and write between 2 and 4 insert statements. (that's the theory at least)
Follow-up to Greg's correct answer... here is the actual SQL script I used:
INSERT INTO aspnet_Users
SELECT * FROM Restored.dbo.aspnet_Users WHERE UserId NOT IN (SELECT UserId FROM aspnet_Users)
INSERT INTO aspnet_Membership
SELECT * FROM Restored.dbo.aspnet_Membership WHERE UserID NOT IN (SELECT UserID FROM aspnet_Membership)
INSERT INTO aspnet_Profile
SELECT * FROM Restored.dbo.aspnet_Profile WHERE UserID NOT IN (SELECT UserID FROM aspnet_Profile)
INSERT INTO aspnet_UsersInRoles
SELECT * FROM Restored.dbo.aspnet_UsersInRoles WHERE UserID NOT IN (SELECT UserID FROM aspnet_UsersInRoles)