SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
create database row_level use row_level create table main_test (id int identity(1,1),name varchar(20)) Create login Alya with password='1234' create user a for login Alya --1-Create the Security Predicate Function: CREATE FUNCTION dbo.MySecurityPredicate(@user int) RETURNS TABLE WITH SCHEMABINDING AS RETURN SELECT 1 AS Result WHERE @user between 2 and 4 and USER_NAME()='a' ; --2--Create a Security Policy: CREATE SECURITY POLICY MySecurityPolicy ADD FILTER PREDICATE dbo.MySecurityPredicate(id) ON main_test WITH (STATE = ON); ---------------------Cleanup (Optional): DROP SECURITY POLICY SalesDataPolicy; DROP FUNCTION MySecurityPredicate ----------check if there are security_policies and security_predicates SELECT * FROM sys.security_policies; SELECT * FROM sys.security_predicates; --------------------------------Alter SecurityPredicate ALTER FUNCTION dbo.MySecurityPredicate (@UserId INT) RETURNS TABLE WITH SCHEMABINDING AS RETURN SELECT 1 AS Result WHERE @UserId=2 and USER_NAME()='a' ; ----------------------Alter the Security Policy ALTER SECURITY POLICY MySecurityPolicy ADD FILTER PREDICATE dbo.MySecurityPredicate(id) ON dbo.main

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear